我有以下模型,视图和控制器.
模型
public class Person {
public string Name;// { get; set; }
public string Occupation { get; set; }
public int Salary { get; set; }
public int[] NumArr { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图
<input type="button" id="Test" value="Test" />
@section Javascript{
<script type="text/javascript">
$(function () {
$("#Test").click(function () {
var data = { Name: "Michael Tom", Occupation: "Programmer", Salary: 8500, NumArr: [2,6,4,9] };
var url = "Home/GetJson";
$.ajax({
url: url,
dataType: "json",
type: "POST",
data: data,
traditional: true, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码对我的MVC模型进行编码,但警报消息为我提供了一个空值.我不确定它为什么给我一个空值,因为这是一个创建表单.我正在尝试从这个创建一个模型,我的HTML代码具有以下外观:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" id="submit" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index") …Run Code Online (Sandbox Code Playgroud) 我有一份就业记录列表,您也可以使用局部视图从同一页面添加就业记录.
下面是jobs.cshtml,它包含记录列表的部分视图,以及一个部分视图,用于添加一个出现在模式弹出窗口中的新记录.
<h2>Employment Records</h2>
@{Html.RenderPartial("_employmentlist", Model);}
<p>
<a href="#regModal" class="btn btn_b" rel="fancyReg">Add New Record</a>
</p>
<div style="display:none">
<div id="regModal">
@{Html.RenderPartial("_AddEmployment", new ViewModelEmploymentRecord());}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
继承人部分视图_AddEmployment.cshtml
@using (Html.BeginForm("AddEmployment, Application"))
{
@Html.ValidationSummary(true)
<div class="formEl_a">
<fieldset>
<legend></legend>
<div class="sepH_b">
<div class="editor-label">
@Html.LabelFor(model => model.employerName)
</div>
etc....etc....
</fieldset>
</div>
<p>
<input type="submit" class="btn btn_d" value="Add New Record" />
</p>
}
Run Code Online (Sandbox Code Playgroud)
并且我的应用程序控制器:
[HttpPost]
public ActionResult AddEmployment(ViewModelEmploymentRecord model)
{
try
{
if (ModelState.IsValid)
{
Add Data.....
}
}
catch
{
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
编译时,为表单生成以下html:
<form …Run Code Online (Sandbox Code Playgroud)