当我在以下视图上单击Create
按钮时:
@model IEnumerable<BasicApplication.Address>
@{
ViewBag.Title = "Index";
Person client = ViewBag.client;
}
<h2>Index</h2>
<div>
<h4>Person</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Person.first_name)
</dt>
<dd>
@Html.Raw(client.first_name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Person.last_name)
</dt>
<dd>
@Html.Raw(client.last_name)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
我遇到以下错误:
参数字典在'BasicApplication.Controllers.AddressController'中的方法'System.Web.Mvc.ActionResult Create(Int32)'中包含非空类型'System.Int32'的参数'id'的空条目>
我的控制器:
public class AddressController : Controller
{
ClientsDataContext cdc = new ClientsDataContext();
// GET: Address/Create
public ActionResult Create(int id) // person id
{
Person client = cdc.getPerson(id);
ViewBag.client = client;
return View();
}
// POST: Address/Create
[HttpPost]
public ActionResult Create(int id, FormCollection collection)
{
try
{
// TODO: Add insert logic here
Address newAdress = new Address()
{
description = collection["description"],
street_address = collection["street_address"],
city = collection["city"],
province = collection["province"],
postal_code = collection["postal_code"],
country = collection["country"],
person_id = id,
};
cdc.Addresses.InsertOnSubmit(newAdress);
cdc.SubmitChanges();
return RedirectToAction("Index", new { id = id });
}
catch
{
Person client = cdc.getPerson(id);
ViewBag.client = client;
return View();
}
}
}
我在create方法中提供了一个ID,不确定为什么会这样。我几天前才刚开始使用MVC,所以我很新。
答案 0 :(得分:0)
问题是您的Create
操作链接缺少id
参数值。因此,您的Create
ActionLink
应该如下:
@Html.ActionLink("Create New", "Create", new {id = client.person_id })