Name of dropdowlist is renamed automatically? - asp.net-mvc-2

I've a problem with DropDownlist Name in ASP.NET MVC
In my EditorTemplate, I've
<%: Html.DropDownList("PoolGeometry",Model.selectVm.PoolGeometry, new { id = "poolgeometry" })%>
In generate html, I've
<select name="Pool.PoolGeometry" id="poolgeometry">
Normally, "PoolGeometry" is a field in db. If my dropdownlistname has the same name, selected value is value of field.
I don't understand this automatic rename!
EDIT :
Name is dependent on EditorTemplate :
if EditorTemplate called like this:
<%: Html.EditorFor(model => model.Pool,"SwimmingPool","")%>
Name of dropdownlist is "PoolGeometry" and selectedvalues are ok.
But if it is called like this:
<%: Html.EditorFor(model => model.Pool,"SwimmingPool")%>
Name of dropdownlist is "Pool.PoolGeometry"

Related

Custom nested form type easyadmin

I have an entity User and an entity Address which are in OneToOne relationship. I would like to display the address type in the User Crud in EasyAdmin, and I don't find a way to do just like Symfony ->add('address', AddressType::class).
I tried the following options:
CollectionField::new('address')
->setEntryIsComplex(true)
->setEntryType(AddressType::class)
->setFormTypeOptions([
'by_reference' => false,
'required' => true
]),
But this makes the user able to add as many as addresses he wants, although I just want one.
AssociationField::new('address')->hideOnIndex()
This one makes the user choose an existing address in a list. That's not an embed of a form type.
Does anyone have an idea?
The solution I found is as follows:
Create Address Field like this
<?php
namespace App\Field;
use App\Form\AddressType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
final class AddressField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = 'Address'): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
// this template is used in 'index' and 'detail' pages
->setTemplatePath('admin/field/address.html.twig')
// this is used in 'edit' and 'new' pages to edit the field contents
// you can use your own form types too
->setFormType(AddressType::class)
->addCssClass('field-address')
;
}
}
And then use it in your User Crud Controller
public function configureFields(string $pageName): iterable
{
// Other fields
yield AddressField::new('address'); // Your new address field
}
templates/admin/field/address.html.twig template would be like this
{% if field.value is defined %}
<dl class="row justify-content-center">
<dt class="col-3 text-right">City</dt>
<dd class="col-9">{{ field.value.city }}</dd>
<dt class="col-3 text-right">Address 1</dt>
<dd class="col-9">{{ field.value.address1 }}</dd>
<dt class="col-3 text-right">Address 2</dt>
dd class="col-9">{{ field.value.address2 }}</dd>
</dl>
{% endif %}

How to conditionally display variables with EJS

This question may seem duplicate, but checking out the related questions didn't give satistfying answers.
Is there a better way of outputing (writing) variables in EJS, other then the commonly suggested
<% if (user) { %><%= user.name %><% } %>
I am searching for something like
<% if (user) {write(user.name)} %>
The closest thing I found was
<%= user.name ? user.name : '' %>
in this good post from 2015: https://blog.joeandrieu.com/2013/10/16/how-to-conditionally-display-variables-with-ejs/, but this approach fails, at least in Node 10, as I get Cannot read property 'name' of undefined. Am I doing it wrong? Or is there another way?
Check if user object is undefined. Use this:
<%= typeof user != 'undefined' ? user.name : '' %>
Alternatively you can attach your user object on locals , so you can use it this way:
<%= user.name ? user.name : '' %>

collection_radio_buttons: how do you wrap a tag around each radio button?

collection_radio_buttons() is defined in the rails 5.1 docs like this:
collection_radio_buttons(
method, collection,
value_method,
text_method,
options = {},
html_options = {}, &block
)
There is no explanation in the docs for what the options argument is. The simple_form docs say that there is an option called item_wrapper_tag.
I've been trying this:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
{item_wrapper_tag: :div} #<=== HERE *****
)
%>
<% end %>
I've tried every combination of symbols and strings for the key, item_wrapper_tag, and the value, div, and nothing succeeds in wrapping each radio button in a div.
Does anyone know if rails has a similar option as item_wrapper_tag?
Okay, I figured it out:
<%= form_for(:an_article, url: "blah") do |f| %>
<%= f.collection_radio_buttons(
:author_id, Author.all,
:id,
:name_with_initial,
) do |b|
%>
<div>
<%= b.radio_button %>
<%= b.label %>
</div>
<% end %> #collection_radio_buttons do block
<% end %> #form_for do block
radio_button and label are builtin methods for the |b|uilder object:
The argument passed to the block is a special kind of builder for this
collection, which has the ability to generate the label and radio
button for the current item in the collection... Using it, you can
change the label and radio button display order or even use the label
as wrapper...
Additional info:
collection_radio_buttons(object, method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
collection: For each element in collection, a radio button and label tag is created.
value_method: Called on each element in collection, and the return value is assigned to
the value attribute of the radio button.
object.method: If the return value of object.method is equal to the value attribute of a radio button,
the radio button gets a checked="checked" attribute.
text_method: Called on each element in collection, and the return value is used as
the text for the label tag.
options: Unknown purpose.
html_options: Used to specify additional html attributes for the radio button, e.g. {class: 'group1'}
When you use form_for(), the object argument is the object encapsulated by f, so you omit the object argument:
f.collection_radio_buttons(method,
collection,
value_method, text_method,
options={}, html_options={}, &block)
and method is called on this object:
|
V
form_for(:an_article, url: "blah") do |f|
Try using the gem simple_form for your forms. Then the code below should work already.
Add gem simple_form in your Gemfile.
Run bundle install
Run rails generate simple_form:install
Then create a simple_form in your view that would look like this:
<%= simple_form_for #post do |f| %>
<%= f.collection_radio_buttons( :author_id, Author.all, :id, :name_with_initial, item_wrapper_tag: :div) %>
<% end %>
Note: I just followed the form from the collection_radio_buttons from APIDock.
This might do the trick. :)

searchresult usin sphinx

i am using sphinx beta and thinking-sphinx to perform search functionality..the search i
wann should search for the word i entered in the database.which is populated with data already.
so in my application Mydoc,i got articlesmodel and controller.. articles is db is populated with data.
i wann to search for the data in articles..so far i ve done with following things but not getting the search result
=> created new search controller
def index
#articles = Article.search params[:search]
end
=> articles.rb(model)
define_index do
indexes name, :sortable => true
indexes description
indexes title, :sortable => true
end
def self.search(search)
ThinkingSphinx::Search
end
=> searches/index.html.erb
<%= form_tag do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", :name => nil %>
</p>
<% end %>
BUT WEN I CLICK ON THE SEARCH BUTTON ITS ASKING FOR CREATEACTION ?????
PLEASE COULD U HELP TO GET SEARCH RESULT
Your form is making a POST request to your SearchController, and that is interpreted as a call to the create action. Try using a GET request instead with your form - something like this:
<%= form_tag '', :method => :get do %>

How to preselect item in Html.DropDownlListFor()

How can i preselect item in Html.DromDownListFor() ?
i have code in view which inserts items to DropDownListFor
<div class="editor-field">
<%var mesta = new List<SelectListItem>();
SelectListItem aa = new SelectListItem();
aa.Text = "---------VYBER MESTO---------";
aa.Value = "0";
mesta.Add(aa);
foreach (var item in Model.MestoTbl)
{
SelectListItem a = new SelectListItem();
a.Text = item.Mesto;
a.Value = item.MestoId.ToString();
mesta.Add(a);}%>
<%: Html.DropDownListFor(model => model.Mesto.MestoId, mesta)%>
<%: Html.ValidationMessageFor(model => model.Mesto.MestoId)%>
</div>
this inserts 2 values MestoId & Mesto ....when i click on some database record (edit field)
example =>
Name Surname Mesto
--------------------
Peter Malik Snina
Snina => Mestoid = 2
I wanna get ....
if i click to edit record of Peter Malik the Html.DropDownListFor automatically preselect item Snina in list.
You could use the SelectList constructor. See here.
thanks now it is working.
<%: Html.DropDownListFor(model => model.Mesto.MestoId, new SelectList(mesta, "Value", "Text", Model.Ziak.MestoId))%>