【发布时间】:2016-08-28 20:55:43
【问题描述】:
我有一个公司模型,它有如下所示的员工列表模型
public class Company
{
[Required]
[Display(Name = "Company Name")]
public string Name { get; set; }
public List<EmployeeModel> Managers { get; set; }
}
以及下面的Employee模型
public class EmployeeModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
我的父视图如下所示
@using (Html.BeginForm("CompanySignupSuccess", "Home", FormMethod.Post, new { @class = "horizontal-form", role = "form", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
<div>
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "control-label" })<span class="required">*</span>
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
<div class="form-group">
<label for="file">Logo:</label>
<input type="file" name="logo" id="logo" accept=".png,.jpg,.jpeg" />
</div>
<div id="managerList">
<div id="editorRowsManagers">
@foreach (var item in Model.Managers)
{
@Html.Partial("DetailsView", item)
}
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default pull-right" value="Send" />
</div>
</div>
}
以及下图的局部视图
@model yourAssembly.EmployeeModel
<div style="border:1px solid;margin:20px; padding:10px;">
Manager Details:
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "control-label" }) <span class="required">*</span>
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Phone, new { @class = "control-label" }) <span class="required">*</span>
@Html.TextBoxFor(m => m.Phone, new { @class = "form-control phoneno" })
</div>
</div>
当我单击提交按钮时,转到控制器的模型确实只填写了名称和徽标属性,并且列表对象(经理)为空,所以我不确定我在这里缺少什么。顺便说一句,我使用了员工列表,因为我想通过“添加”按钮添加更多员工,而“添加”按钮只会呈现另一个部分视图。
public ActionResult CompanySignupSuccess(Company model)
{
if (ModelState.IsValid)
{
//do some process
}
else
{
ModelState.AddModelError("", "Invalid Data entered.");
}
// If we got this far, something failed, redisplay form
return View("CompanySignup", Model);
}
当点击提交按钮时,任何人都可以帮助我如何发送子列表对象以及父类的一些属性。
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-4 form-submit