开发者

ASP.NET MVC : dropdown positioning and save

I have this in my view :

@Html.DropDownList("LookupTypeImmobilier", new SelectList(Model.LkpTypeImmobilier, "Code", "Value", Model.LookupTypeImmobilier.Code))

The lookup class has the properties : Id, FR, NL, Value and Code. The Value is text show in the dropdown.

LkpTypeImmobilier is the list and LookupTypeImmobilier is the selected, the values are correct (see the pictures) but the dropdown is not setted.

When I select and I post like this :

var jqxhr = $.post("Controller/Action", $("form").serialize(),
function (data) {
}); 

The values coming from textbox, textarea are posted and I get them in the controller but not the dropdown.

Do you have an idea ?

Thanks,

ASP.NET MVC : dropdown positioning and save

ASP.NET MVC : dropdown positioning and save

Update 1 (ShankarSangoli request)

<select id="LookupTypeImmobilier" name="LookupTypeImmobilier"><option value="0">----</option>
<option value="1">habitation principale / logement familial</option>
<option value="2">résidence secondaire</option>
<option value="3">immeuble de rapport</option>
<option value="4">terrain à bâtir</option>
<option value="5">terre agricole</option>
<option value="6">bois</option>
</select>

Update 2 Tried with this but not work (null when I post and no positioning)

@Html.DropDownListFor(m => m.LookupTypeImmobilier, new SelectList(Model.LkpTypeImmobilier, "Code", "Value", Model.LookupTypeImmobilier))
开发者_运维技巧

Update 3

No solution then I use a workaround. I post with jQuery field by field and not the model. In edition I set the dropdown list position by jQuery. It's really not the right way but work, I hope find a solution.


The following should work:

@Html.DropDownListFor(
    m => m.LookupTypeImmobilier, 
    new SelectList(Model.LkpTypeImmobilier, "Code", "Value")
)

Now in order to set a default value for the dropdownlist ensure that your controller action sets a value of the LookupTypeImmobilier on the view model:

var model = new MyViewModel
{
    // Preselect the third element which in the rendered HTML has value="3"
    LookupTypeImmobilier = "3",
    LkpTypeImmobilier = ...
}
return View(model);

Make sure that the value you are setting the LookupTypeImmobilier property to exists in the LkpTypeImmobilier collection. This will automatically preselect the corresponding element of the dropdownlist. And it should correspond to the Code property, not the Value.

As far as getting the selected value back in your POST controller action is concerned, simply use this same view model as action argument:

[HttpPost]
public ActionResult Foo(MyViewModel model)
{
    // model.LookupTypeImmobilier will contain the selected value here
    ...
}


Explain please, "The lookup class has the properties : Id, FR, NL, Value and Code", but your view has @Html.DropDownList("LookupTypeImmobilier"...

can you write something like:

@Html.DropDownListFor(m => m.Value,new SelectList(/**/))

Sorry, maybe I understand something wrong :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜