2017-10-11 54 views
0

我不明白为什么模型绑定不验证数据。我的代码工作正常,没有模型联编程序。我试图调用的操作是Enter。所以,网址是这样的。 (本地主机:5×10 17 /客户/输入)。我的代码如下。为什么数据注解与asp.net MVC5中的model-binder不起作用?

客户Controller.cs

public class CustomerController : Controller 
{ 
    // GET: Customer 
    public ActionResult Load() 
    { 
     Customer obj = new Customer() 
     { 
      CustomerCode = "1001", 
      CustomerName = "Rezwan" 
     }; 
     return View("Customer", obj); 
    } 
    public ActionResult Enter() 
    { 
     return View("EnterCustomer"); 
    } 

    public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer obj) 
    { 
     if (ModelState.IsValid) 
      return View("Customer", obj); 
     else 
      return View("EnterCustomer");  
    } 
} 

EnterCustomer.cshtml

@{ 
 
    Layout = null; 
 
} 
 

 
<!DOCTYPE html> 
 

 
<html> 
 
<head> 
 
    <meta name="viewport" content="width=device-width" /> 
 
    <title>EnterCustomer</title> 
 
</head> 
 
<body> 
 
    <div> 
 
     <form action="Submit" method="post"> 
 
      Customer Name - <input name="txtCustomerName" type="text" /> 
 
      Customer Code - <input name="txtCustomerCode" type="text" /> 
 
      <input id="Button1" type="submit" value="submit" /> 
 
     </form> 
 
     @Html.ValidationSummary() 
 
    </div> 
 
</body> 
 
</html>

Customer.cs

public class Customer 
{ 
    [Required] 
    [RegularExpression("^[A-Z]{3,3}[0-9]{4,4}$")] 
    public string CustomerCode { get; set; } 

    [Required] 
    [StringLength(10)] 
    public string CustomerName { get; set; } 
} 

CustomerBinder.cs

public class CustomerBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     HttpContextBase objContext = controllerContext.HttpContext; 
     string CustCode = objContext.Request.Form["txtCustomerCode"]; 
     string CustName = objContext.Request.Form["txtCustomerName"]; 

     Customer obj = new Customer() 
     { 
      CustomerCode = CustCode, 
      CustomerName = CustName 
     }; 

     return obj; 

    } 
} 

Customer.cshtml

@model HelloWorld.Models.Customer 
 

 
@{ 
 
    Layout = null; 
 
} 
 

 
<!DOCTYPE html> 
 

 
<html> 
 
<head> 
 
    <meta name="viewport" content="width=device-width" /> 
 
    <title>Customer</title> 
 
</head> 
 
<body> 
 
    <div> 
 
     Name - @Model.CustomerName <br/> 
 
     Code - @Model.CustomerCode 
 
    </div> 
 
</body> 
 
</html>

请帮我一把。我是新的asp.net语言。

+0

如果你在'CustomerBinder.BindModel'中放置了一个断点,那么这个代码是否被命中? – DiskJunky

+1

您是否启用了不显眼的客户端验证? –

+0

你说你试图调用控制器动作输入,但你的表单在EnterCustomer.cshtml映射到提交动作 – Fran

回答

0

这里确实没有必要创建一个自定义模型绑定器。你也会反对很多mvc框架约定。您通常会看到类似Create(Get)和Create(Post)这样的动作对,其视图也是名称Create.cshtml

因此,在mvc中使用脚手架框架,您可以从您的Customer model类生成所需的所有内容。

产生的视图(Create.cshtml):在CustomerController

// GET: Customer/Create 
    public ActionResult Create() 
    { 
     return View(); 
    } 

    // POST: Customer/Create 
    [HttpPost] 
    public ActionResult Create(Customer customer) 
    { 

     if (!ModelState.IsValid) 
     { 
      return View(customer); 
     } 

     //do work 
     return RedirectToAction("Index"); 
    } 

创建

@model Scratch.Mvc.Models.Customer 

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>View</title> 
</head> 
<body> 
    @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.CustomerCode, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.CustomerCode, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.CustomerCode, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.CustomerName, "", 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" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 
</body> 
</html> 

控制器动作所以后期控制器动作以客户对象,并使用默认的模型绑定器绑定HTML表单的字段与对象。这是可行的,因为EditorFor将生成正确的html表单字段名称,该名称将自动绑定到Customer类上的同一个命名属性。

POST操作中的ModelState检查然后返回与包含的验证信息相同的视图,然后使用ValidationMessageFor辅助方法绑定验证信息。

它也重定向回索引页(此处未定义)以符合PRG模式。

相关问题