Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DropDownListFor binding
Message
 
 
À
15/01/2013 02:44:21
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Versions des environnements
Environment:
C# 5.0
Divers
Thread ID:
01562476
Message ID:
01562580
Vues:
29
>>>Given:
 public class LLProduct
>>>  {
>>>    public Guid Id { get; set; }
>>>    public String Name { get; set; }
>>>  }
>>>
>>>  public class ProductsViewModel
>>>  {
>>>    public LLProduct Product { get; set; }
>>>  }
and an MVC action
      Guid id = Guid.NewGuid();
>>>      List<LLProduct> list = new List<LLProduct>
>>>        {
>>>          new LLProduct{Id=Guid.NewGuid(),Name="One"},
>>>          new LLProduct{Id = id,Name = "Two"}
>>>        };
>>>      ViewBag.Selector = new SelectList(list, "Id", "Name");
>>>
>>>      var p = new LLProduct();
>>>      p.Id = id;
>>> 
>>>      ProductsViewModel pvm = new ProductsViewModel{Product = p};
>>>      return View(pvm);
then '@Html.DropDownListFor(x=>x.Product.Id,(SelectList)ViewBag.Selector)
>>>' selects the right item ("Two")
>>>
>>>If, however, I use 'p' as the parameter to the view (and change the model in the cshtml) then '@Html.DropDownListFor(x=>x.Id,(SelectList)ViewBag.Selector) shows "One" (no value selected in the generated html) ?
>>
>Hi
>
>>What do you mean by p as the parameter to the view? What exactly is the problem - the list doesn't show both entries?
>
>Above code with this razor view:
@model  ProductsViewModel
>
>@using (Html.BeginForm("ProductLists", "Products"))
>{
>  @Html.DropDownListFor(x=>x.Product.Id,(SelectList)ViewBag.Selector)
>}
Shows the list with "Two" as expected. Same controller code but with the return changed to
return View(p);
and view changed to:
@model  LLProduct
>
>@using (Html.BeginForm("ProductLists", "Products"))
>{
>  @Html.DropDownListFor(x=>x.Id,(SelectList)ViewBag.Selector)
>}
shows "One" - second item is not selected.......
>
>
>,
Yes, I see now - strange. But I think your first way is better anyway, right? E.g. use ViewModel instead of using the product as a model directly.

BTW, do you know how can I pass the model to a view using jquery (if this is possible at all)?

Say, someone suggested me to try this code:
var ClientName;
                ClientName =$('.trSelected td:eq(2)').text();
                var url = '/Client/GetDetails/' ;

                $.getJSON(url, id, function (data) {
                  //  setFormControls(data.Id, data.Role, data.Location, data.JobType, data.Description);
                });
The problem now is that I can not directly use setFormControls as the above because my form is much more complex and I don't even expose Ids for controls. This is my view:
@using WebDemo.Helper
@model CardNumbers.Models.ClientViewModel
 @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "sform", title = "Client Info" }))
    
    {    
    <fieldset>
        <legend>Client Info</legend>

        @Html.ValidationSummary(true)
       
        @Html.HiddenFor(m => m.ClientId)
        @Html.EditorFor(m => m.Number, EditorTemplate.TextBox)

        @Html.EditorFor(m => m.Name, EditorTemplate.TextBox)

        @Html.EditorFor(m => m.Client.Address, EditorTemplate.EditBox)

        <div id="ContactsInfo">

            <div id="Contact1">

                @Html.EditorFor(m => m.Client.Contact1)

            </div>

            <div id="Contact2">

                @Html.EditorFor(m => m.Client.Contact2)
            </div>
        </div>

        <div id="SaveCancel" class="float-right">
            <button id="btnSave">Save</button>
            <button type="reset" id ="btnCancel" name="Reset">Cancel</button>
        </div>
    </fieldset>

 }
So, do you see how can I associate returned json data with that view?

Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform