开发者

MVC Strogly Typed Partial Views Model Binding

I am new to MVC so here is my issue. I have a parent view and a partial view which is rendered inside it. As a model I pass an IEnumerable to the parent view. I iterate thru the list and for each item i render the partial view having the list item as model. On the partial view I have a form which on a submit triggers a child action which accepts as parameter the type of the list. My issue is that the param comes always with it's values null.

These are my domain entities.

public class Contact
    {
        [Key]
        public int IdContacts { get; set; }
        public int UserId { get; set; }
        public int CreatedByUserId { get; set; }
        public int UpdatedByUserId { get; set; }
        public int AddressId { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public long HomePhone { get; set; }
        public long? WorkPhone { get; set; }
        public bool IsRelated { get; set; }
        public bool IsEmergency { get; set; }
        public bool IsDeceased { get; set; }
        public string Relationship { get; set; }
        public DateTime EntryDate { get; set; }
        public DateTime? ChangeDate { get; set; }
    }

 public class Address
    {
        [Key]
        public int IdAddresses { get; set; }
        public int CountryId { get; set; }
        public int StateId { get; set; }
        public int CreatedByUserId { get; set; }
        public int UpdatedByUserId { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public long PostalCode { get; set; }
        public string OfficeOrApt { get; set; }
        public int AreaGroup { get; set; }
        public int? SuperUserId { get; set; }
        public DateTime EntryDate { get; set; }
        public DateTime? ChangeDate { get; set; }
    }

This is my view model

public class ContactModel
    {
        public Contact Contact { get; set; }
        public Address Address { get; set; }
        public bool IsEditMode { get; set; }
    }

This is my parent view

@model IEnumerable<Cricket.WebUI.Models.ContactModel>
@{
    ViewBag.Title = "Contacts";
}
<h2 align="center">
    Contacts</h2>
<div class="iggr_container">
    <div class="iggr_clear">
    </div>
    @foreach (var contact in Model)
    {
        ViewDataDictionary dictionary = new ViewDataDictionary();
        string guid = (Guid.NewGuid()).ToString();
        dictionary.Add("prefix", guid);

        @Html.Hidden("Contact.Index", guid)
        @Html.Hidden("Address.Index", guid)
        @Html.Partial("ContactSummary", contact, dictionary)
        <hr style="width: 385px;" align="left" /> 
    }
</div>
开发者_如何转开发

This is my partial view

@model Models.ContactModel
<div class="iggr_clear">
</div>
@if (!Model.IsEditMode)
{
    var prefixContact = "Contact[" + ViewData["prefix"] + "].";
    var prefixAddress = "Address[" + ViewData["prefix"] + "].";

    using (Html.BeginForm("Edit", "Contacts", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        TempData["ContactModelObject"] = Model;
    <div>
        <b>
            @Html.Hidden(prefixContact + "FirstName", Model.Contact.FirstName);
            @Html.Hidden(prefixContact + "LastName", new { name = "Contact" + ViewData["prefixContact"] + ".LastName" })
            @Html.LabelFor(m => m.Contact.FirstName, Model.Contact.FirstName)
            &nbsp;
            @Html.LabelFor(m => m.Contact.LastName, Model.Contact.LastName)
        </b>
        <div>
            <span>Home Phone:</span>
            @Html.Hidden(prefixContact + "HomePhone", Model.Contact.HomePhone)
            @Html.LabelFor(m => m.Contact.HomePhone, Model.Contact.HomePhone.ToString())</div>
        <div>
            <span>Work Phone:</span>
            @Html.Hidden(prefixContact + "WorkPhone", Model.Contact.WorkPhone)
            <span>
                @if (Model.Contact.WorkPhone == null)
                {
                    @:N/A
            }
                else
                {
                    @Html.LabelFor(m => m.Contact.WorkPhone, Model.Contact.WorkPhone.ToString())
                }
            </span>
        </div>
        <div>
            @Html.Hidden(prefixAddress + "Street", Model.Address.Street)
            @Html.LabelFor(m => m.Address.Street, Model.Address.Street)
        </div>
        <div>
            @Html.Hidden(prefixAddress + "City", Model.Address.City)
            @Html.Hidden(prefixAddress + "PostalCode", Model.Address.PostalCode)
            @Html.LabelFor(m => m.Address.City, Model.Address.City)&nbsp;&nbsp;@Html.LabelFor(m => m.Address.PostalCode, Model.Address.PostalCode.ToString())
        </div>
        @Html.Hidden(prefixContact + "IsRelated", Model.Contact.IsRelated)
        @if (Model.Contact.IsRelated)
        {
            <b>Family</b>
            if (Model.Contact.IsEmergency || Model.Contact.IsDeceased)
            {
            <b>/</b>
            }
        }
        @Html.Hidden(prefixContact + "IsEmergency", Model.Contact.IsEmergency)
        @if (Model.Contact.IsEmergency && !Model.Contact.IsDeceased)
        { 
            <b>Emergency</b>
        }
        @Html.Hidden(prefixContact + "IsDeceased", Model.Contact.IsDeceased)
        @if (Model.Contact.IsDeceased)
        { 
            <b>Deceased</b>
        }
        <input type="submit" name="button" value="Edit" class="iggr_button_edit" style="margin-left: 150px;" />
    </div>
    }
}

And finally my controller class

public class ContactsController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        ContactsRepository repository = new ContactsRepository();

        CDE.User user = (from u in repository.Users where u.IdUsers == 1 select u).FirstOrDefault();

        var contacts = (from u in repository.Users
                       join c in repository.Contacts on new { UserId = u.IdUsers } equals new { UserId = c.UserId }
                       join a in repository.Addresses on new { AddressId = c.AddressId } equals new { AddressId = a.IdAddresses }
                       select new ContactModel()
                       {
                           Contact = c,
                           Address = a
                       }).AsEnumerable();

        return View("Contacts", contacts);
    }

    [HttpPost]
    [ActionName("Edit")]
    [@ActionFilter(ActionInvokerType = "button", ActionInvokerValue = "Edit")]
    public ViewResult Edit(ContactModel contact)
    {
        //Here contact.Contact comes as null same for contact.Address
        return View("Contacts", null);//Ignore this line
    }
}


I have a feeling that your prefix structure is interfering with the binding that normally happens in MVC. You could use a custom model binding implementation, or find a way to do without your prefixes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜