我創建了一個表單和一個按鈕 SAVE。我還創建了一個操作方法 CREATE,當我們單擊 SAVE 按鈕時將呼叫它。我現在想查看模型系結,但是當我填寫表格并單擊保存按鈕時,它會顯示以下錯誤訊息。它顯示錯誤訊息 請參閱附件代碼
@using (Html.BeginForm("Create", "Customer"))
{
<div class="form-group">
@Html.LabelFor(m=>m.Customer.Name)
@Html.TextBoxFor(m=>m.Customer.Name, new { @class="form-control"})
</div>
<div class="form-group">
@Html.LabelFor(m=>m.Customer.BirthDate)
@Html.TextBoxFor(m=>m.Customer.BirthDate, new { @class="form-control"})
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m=>m.Customer.IsSubscribedToNewsLetter) Subscribed to Newsletter?
</label>
</div>
<div class="form-group">
@Html.LabelFor(m=>m.Customer.MembershipTypeId)
@*List of items*@
@Html.DropDownListFor(m=>m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes,"Id","Name"),"Select",new { @class="form-control"})
</div>
<button type="submit" class="btn btn-primary">Save</button>
}
下面是控制器的創建操作方法
[HttpPost]
public ActionResult Create(Customer customer)
{
return View();
}
uj5u.com熱心網友回復:
嘗試添加屬性路由
[Route("~/customer/create")]
public ActionResult Create(Customer customer)
并將模型添加到視圖頂部
@model Customer
@using (Html.BeginForm("Create", "Customer", FormMethod.Post)) {
如果您使用的是 Net 4.8 或更早版本,則必須配置屬性路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
````
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407648.html
標籤:
