Когда я нажимаю кнопку 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>
Я получаю следующую ошибку:
Словарь параметров содержит пустую запись дляПараметр 'id' с необнуляемым типом 'System.Int32' для метода 'System.Web.Mvc.ActionResult Create (Int32)' в 'BasicApplication.Controllers.AddressController'
Мой контроллер:
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();
}
}
}
Я предоставляю идентификатор в методе создания, не знаю, почему это происходит.Я только начал работать с MVC пару дней назад, поэтому я очень новичок.