Hello to all,
I try to open a modal dialog, pass a model to the dialog and get the model back to the controller. Because I'm not very familiar to ASP.NET and MVC I seached the net and found this:
Bootstrap Modal Dialog - Loading Content from MVC Partial View
So far, so good. I've made some modifications to fit my requirements, but I won't get back the modified values out of the modal view back to my controller.
Some more details: I have models that look like:
public class User
{
public Int32 ID { get; set; }
...
public Contact Contact { get; set; }
...
}
public class Contact
{
public Int32 ID { get; set; }
...
public List<ContactPropertyValue> ContactPropertyValues { get; set; }
public List<ContactProperty> ContactProperties { get; set; }
...
}
public class ContactPropertyValue
{
public Int32 ID { get; set; }
public Int32 UserID { get; set; }
public Int32 ContactPropertyID { get; set; }
public string ContactPropertyValue ( get; set; }
}
public class ContactProperty
{
public Int32 ContactPropertyID { get; set; }
public string ContactPropertyName { get; set; }
}
This structure is implemented to keep compatibility to outlook contact parameters. For example there is a list of ContactProperties
List<ContactProperty> lstContactProperty = new List<ContactProperty> {
new ContactProperty {ContactPropertyID=1, ContactPropertyName="LastName" },
new ContactProperty {ContactPropertyID=2, ContactPropertyName="FirstName" },
new ContactProperty {ContactPropertyID=3, ContactPropertyName="PhoneNumber" },
...}
And out of this I can create a list of ContactPropertyValues for my contacts:
List<ContactPropertyValue> lstContactPropertyValue = new List<ContactPropertyValue> {
new ContactPropertyValue {ID=1, UserID=1, ContactPropertyID=1, ContactPropertyValue="Miller" },
new ContactPropertyValue {ID=2, UserID=1, ContactPropertyID=2, ContactPropertyValue="Marc" },
new ContactPropertyValue {ID=3, UserID=1, ContactPropertyID=3, ContactPropertyValue="123-456" },
new ContactPropertyValue {ID=4, UserID=2, ContactPropertyID=1, ContactPropertyValue="Smith" },
new ContactPropertyValue {ID=5, UserID=2, ContactPropertyID=2, ContactPropertyValue="Lara" },
new ContactPropertyValue {ID=6, UserID=2, ContactPropertyID=3, ContactPropertyValue="123-789" },
... }
Now I give the User model to my edit view where the @Html.ActionLink for the modal dialog is placed.
@model MyApp.Models.User
...
@using (Html.BeginForm("Edit", "User", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
...
<div class="col-sm-1">
@Html.ActionLink("Contact Details", "ViewContactDetails", "User", null, new { @class = "modal-link btn btn-default" })
</div>
...
} @*end using*@
The parital view looks like:
@model MyApp.Models.User
...
@using (Html.BeginForm("ContactDetails", "User", @Model, FormMethod.Post))
{
...
<div class="text-nowrap" id="divContactDetails">@Html.Partial("../Contacts/_ContactDetails", Model.Contact )</div>
} @*end using*@
This construct is done to reuse the view "_ContactDetails". It looks like:
@model MyApp.Models.Contact
<div class="form-group">
<h1>Contact Details</h1>
</div>
...
<div class="form-group">
@Html.Label("Lastname", new { @class = "col-sm-3 control-label" })
<div class="col-sm-6">
@{
propertyId = Model.ContactProperies.Find(x => x.Name == "LastName").ContactPropertyID;
propertyValueId = Model.ContactPropertyValues.Find(y => y.ContactPropertyId == propertyId).ID;
}
@Html.TextBoxFor(m => m.ContactPropertyValues[propertyValueId].ContactPropertyValue, new { @class = "form-control", id = "idLastName" })
</div>
The controller looks like:
public async Task<ActionResult> ViewContactDetails()
{
User user = ... ;
return PartialView("Modals/_mdlContactDetailsNew", user);
}
[HttpPost]
public ActionResult ContactDetails(User user)
{
return RedirectToAction("Edit", user);
}
It all works well - I can open the modal dialog with the parital view in it and I see all my contact properties. But if I modify them and call the save function (ActionResult ContactDetails) there are still the old values set.
Is there anybody who knows the trick to get it work?