I have a simplified test case useful for asking this question: a product can have many Components, a Component can belong to many Products. EF created the classes, I reduced them as follows:
public partial class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Component> Components { get; set; }
}
public partial class Component
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Creating a component is carried out using these controller actions:
public ActionResult Create(int ProductId)
{
Product p = db.Products.Find(ProductId);
Component c = new Component();
c.Products.Add(p);
return PartialView(c);
}
[HttpPost]
public ActionResult Create(Component model)
{
db.Components.Add(model);
db.SaveChanges();
}
and the view returned by the GET method looks like this:
@model Test.Models.Product
<fieldset>
<legend>Product</legend>
<div class="display-label">Name</div>
<div class="display-field">@Model.Name</div>
</fieldset>
@Html.Action("Create", "Component", new {ProductId = Model.Id})
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>
It can be seen that the creation of the component is processed on one page using the above Html.Action- the code for this type should be:
@model Test.Models.Component
@using Test.Models
<script type="text/javascript">
function Success() {
alert('ok');
}
function Failure() {
alert('err');
}
</script>
@using (Ajax.BeginForm("Create", "Component", new AjaxOptions
{
HttpMethod = "Post",
OnSuccess = "Success",
OnFailure = "Failure"
}))
{
<fieldset>
<legend>Components</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
@Html.HiddenFor(x => x.Products.First().Id)
@Html.HiddenFor(x => x.Products)
@foreach (Product p in Model.Products)
{
@Html.Hidden("Products[0].Id", p.Id)
}
@foreach (Product p in Model.Products)
{
@Html.Hidden("[0].Id", p.Id)
}
</fieldset>
<input type="submit" value="go" />
}
. , : model [HttpPost] , .. , . , . , :
model.Products.Add(db.Products.Find(model.Products.First().Id));
, model, id. , , -, , , , , .
* For, . .Products (.Product), x => x.Product.Id, , , x => x.Products.Id, x => x.Products.First().Id, Id ( , , Component.Id, Component.Products[0].Id.
, HiddenFor (, EditorFor):
@Html.HiddenFor(x => x.Products)
- , . x => x.Products.First(), . , * For :
@foreach (Product p in Model.Products)
{
@Html.Hidden("Products[0].Id", p.Id)
, , postback (Products.Count == 0). , [0].Id, . GRR...
, :
@Html.Hidden("ProductId", p.Id)
:
[HttpPost] ActionResult Create(Component model, int ProductId)
eecky. , . - ?
p.s. , , -