在我的MVC应用程序中,我有一个类似下面的模型类
public class ProjectViewModel
{
public PROJECT Project { get; set; }
public bool IsSelected { get; set; }
public COMPANY Companies { get; set; }
public CLIENT Clients { get; set; }
}
我有一个像这样的ViewModel
// POST: /Project/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProjectViewModel project)
{
if (ModelState.IsValid)
{
db.PROJECTs.Add(project.Project);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CLIENT_ID = new SelectList(db.CLIENTs, "ID", "NAME", project.Project.CLIENT_ID);
ViewBag.COMPANY_ID = new SelectList(db.COMPANies, "ID", "NAME", project.Project.COMPANY_ID);
return View(project);
}
ViewModel是我为其创建控制器和视图的视图。我也创建了索引,详细信息,删除和创建视图。索引,详细信息和删除视图只是工作正常但创建控制器模型绑定似乎不起作用。作为控制器Create的输入的ProjectViewModel对象为null。如何将ViewModel作为参数绑定到此对象?
@model IMCCBluePrints.Models.ProjectViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PROJECT</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Project.PROJECT_NO, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Project.PROJECT_NO)
@Html.ValidationMessageFor(model => model.Project.PROJECT_NO)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project.TITLE, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Project.TITLE)
@Html.ValidationMessageFor(model => model.Project.TITLE)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project.DESCRIPTION, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Project.DESCRIPTION)
@Html.ValidationMessageFor(model => model.Project.DESCRIPTION)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project.REMARKS, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Project.REMARKS)
@Html.ValidationMessageFor(model => model.Project.REMARKS)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project.COMPANY_ID, "COMPANY_ID", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("COMPANY_ID", String.Empty)
@Html.ValidationMessageFor(model => model.Project.COMPANY_ID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Project.CLIENT_ID, "CLIENT_ID", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CLIENT_ID", String.Empty)
@Html.ValidationMessageFor(model => model.Project.CLIENT_ID)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这是创建视图
$(function () {
$('#myModal').modal('toggle');
});
答案 0 :(得分:1)
在您的GET操作中实例化您的项目类并尝试:
...
ProjectViewModel.project = new PROJECT();
...
return View(ProjectViewModel);
答案 1 :(得分:0)
表单提交项目与Project类型冲突的问题,因为它是复杂类型,只需将其重命名为模型,例如
public ActionResult Create(ProjectViewModel model)
尝试这个,它会起作用,我自己尝试过。