[英]Model (complex type) won't submit to action
I have some problem posting a form with 'complex type' model: 我在发布带有“复杂类型”模型的表单时遇到一些问题:
I have a Model: 我有一个模特:
public class CircleEditViewModel
{
[Key]
public int CircleId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public bool IsSystem { get; set; }
public class UserInCircle
{
public UserInCircle(User user)
{
this.UserId = user.UserId;
FullName = user.FullName;
}
public int UserId { get; set; }
public byte[] Picture { get; set; }
public string FullName { get; set; }
public bool isInCircle { get; set; }
}
public List<UserInCircle> Users { get; set; }
}
My first problem was that at post event, my Users where null.. so i followed a few posts on here (like MVC- Model Binding on a Complex Type ) to use a for instead of a foreach ,but since i did so, my form won't post anymore: 我的第一个问题是,在发布事件中,我的用户在null处。因此,我在此处关注了几篇文章(例如, 对复杂类型进行MVC模型绑定 ),使用for代替了foreach ,但是由于我这样做了,表格将不再发布:
View: 视图:
@model Wims.Website.ViewModels.CircleEditViewModel
<script type="text/javascript">
$(document).ready(function () {
$.validator.unobtrusive.parse('form');
});
</script>
@using (Ajax.BeginForm(Html.ViewContext.RouteData.Values["Action"].ToString(), null, new AjaxOptions { HttpMethod = "POST", OnSuccess = "SaveDone(data)" }, new { id = "editform" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Circle</legend>
@Html.Label(DateTime.Now.ToString());
<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>
</fieldset>
if (Model.Users != null)
{
for (int i = 0; i < Model.Users.Count; i++)
{
<div class="userDetail">
<div>
<div>
@Html.DisplayFor(model => Model.Users[i].isInCircle);
</div>
<div class="iconDiv">
@Html.Image("~/Content/Images/defaultUser.jpg", Model.Users[i].FullName, null);
</div>
<div>
@Html.TextBoxFor(model => Model.Users[i].FullName)
@Html.HiddenFor(model => Model.Users[i].UserId)
</div>
</div>
</div>
<div style="clear: both"></div>
}
}
@Html.GenerateSecureDataControls(model => model.CircleId)
<input type="submit" value="Save" />
}
My view is rendered as a partial loaded thru ajax (not sure it makes any difference here). 我的视图通过ajax呈现为部分加载状态(不确定此处是否有任何区别)。 Any idea why it won't post?
知道为什么它不发布吗? If i remove all the '[]' like 'Users[0].FullName' to Users0.FullName i will post, but of course it won't be bound.
如果我将所有'[]'删除,例如'Users [0] .FullName'到Users0.FullName,我将发布,但当然不会绑定。
Thanks for your help 谢谢你的帮助
Edit just in case needed: Action: 编辑以防万一:操作:
[HttpPost]
public ActionResult Edit(CircleEditViewModel circleData, FormCollection collection)
{
if (ModelState.IsValid)
{
using (var logic = new CircleLogic())
{
Circle circle = logic.GetCircleById(circleData.CircleId, WebMatrix.WebData.WebSecurity.CurrentUserId);
if (circle == null)
{
return HttpNotFound();
}
else
{
circle.Name = circleData.Name;
logic.UpdateCircle(circle, GetSelectedUser(collection));
}
return PartialView("_CircleAndUsers", GetData(logic, circle.CircleId));
}
}
return this.Json(new { success = false, viewdata = RenderRazorViewToString("_CircleAndUsers", circleData) });
}
Pablo Romeo是对的,我添加了一个默认的ctor,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.