user1404577
user1404577

Reputation:

Passing the values of the DisplayFor to the model binder

I have the following view , which mainly view a list of items as a displayfor:-

        @foreach (var item in Model.Resources) {
            <tr>
                 <td>
                    @Html.DisplayFor(modelItem => item.SystemInfo.MODEL)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RESOURCENAME)
                </td>
                <td>

                     @Html.DisplayFor(modelItem  => item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ComponentDefinition.COMPONENTNAME)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ResourceState.STATEDESC)
                </td>
                 <td id = "@item.RESOURCEID">

        @using (Ajax.BeginForm("CreateOn","VirtualMachine", new AjaxOptions {
         InsertionMode = InsertionMode.Replace,
         UpdateTargetId = item.RESOURCEID.ToString() ,
         LoadingElementId = "progress",
         HttpMethod = "POST"})){
 <span class="f">Hypervisor Server</span>

             @Html.DropDownListFor(model =>model.VirtualMachine.ServerID, ((IEnumerable<t.Models.Server>)ViewBag.Servers).Select(option => new SelectListItem {
                Text = (option == null ? "None" : option.Technology.Tag), 
                Value = option.ServerID.ToString(),
                Selected = (Model != null) && (Model.VirtualMachine != null) && (option.ServerID == Model.VirtualMachine.ServerID)
            }), "Choose...")
    @Html.ValidationMessageFor(model =>model.VirtualMachine.ServerID)
    @Html.Hidden("IT360id", item.RESOURCEID)
            @Html.Hidden("CustomerName",item.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization.NAME)
            @Html.Hidden("SiteName",item.ResourceLocation.SiteDefinition.SDOrganization.NAME)
            @Html.Hidden("ResourceName",item.RESOURCENAME)
         <input type="submit" value="Add To" class="btn btn-primary"/>
        }

But the user can read the data and chose to create the item on our database, using the ajax.beginform. But to do so I need to pass some values of the DisplayFor to the model binder. Currently I have added all the needed data as a hiddenfields, and then I will pass these values to my action method as follow:-

[HttpPost]
        public ActionResult CreateOn(VirtualMachineOnIT360Only vm, long IT360id, string CustomerName, string SiteName, string ResourceName)
        {

I found that my approach is not very reliable and I am trying to figure out a more reliable solution. So can anyone advice please? Thanks

Upvotes: 1

Views: 1552

Answers (1)

hutchonoid
hutchonoid

Reputation: 33306

To post DisplayFor back, I prefer to use hiddenfor instead of hidden like this:

            @Html.HiddenFor(item => item.RESOURCENAME);

Upvotes: 1

Related Questions