2017-08-23 150 views
0

我试图将编辑操作绑定到不起作用的模型。在这里,控制器下面:ASP.NET核心模型绑定

  [Route("[controller]/[action]")] 
       public class CustomerController : Controller 
       { 
        private readonly IUnitOfWork _unitOfWork; 
        public CustomerController(IUnitOfWork unitOfWork) 
        { 
         _unitOfWork = unitOfWork; 
        } 

        [HttpGet("{_customerCode}")] 
        public IActionResult Edit(int _customerCode) 
        { 
         var customer = _unitOfWork.Customers.getCustomerByCode(_customerCode); 
         var customerDTO = Mapper.Map<CustomerModelDTO>(customer); 
         return View(customerDTO); 

        } 

        [HttpPost] 
        public IActionResult Edit(CustomerModelDTO model) 
        { 

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

         var _customer = _unitOfWork.Customers.getCustomerByCode(model.CustomerCode); 

         if (_customer==null) 
         { 
          return NotFound(); 
         } 


         Mapper.Map(model, _customer); 
         _unitOfWork.Complete(); 

         return RedirectToAction("Detail", new { customerCode = _customer.CustomerCode }); 
        }   
       } 

这里是我使用的观点:

  @model Almiz.Dtos.CustomerModelDTO 

      <form asp-action="Edit" method="post"> 
       <div class="form-horizontal"> 
        <h4>CustomerModelDTO</h4> 
        <hr /> 
        <div asp-validation-summary="ModelOnly" class="text-danger"></div> 
        <div class="form-group"> 
         <label asp-for="CustomerCode" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="CustomerCode" class="form-control" /> 
          <span asp-validation-for="CustomerCode" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="FirstName" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="FirstName" class="form-control" /> 
          <span asp-validation-for="FirstName" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="MiddleName" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="MiddleName" class="form-control" /> 
          <span asp-validation-for="MiddleName" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="LastName" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="LastName" class="form-control" /> 
          <span asp-validation-for="LastName" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="Telephone" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="Telephone" class="form-control" /> 
          <span asp-validation-for="Telephone" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="Mobile" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="Mobile" class="form-control" /> 
          <span asp-validation-for="Mobile" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <label asp-for="Email" class="col-md-2 control-label"></label> 
         <div class="col-md-10"> 
          <input asp-for="Email" class="form-control" /> 
          <span asp-validation-for="Email" class="text-danger" /> 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-md-offset-2 col-md-10"> 
          <input type="submit" value="Save" class="btn btn-default" /> 
         </div> 
        </div> 
       </div> 
      </form> 

      <div> 
       <a asp-action="Index">Back to Index</a> 
      </div> 

      @section Scripts { 
       @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} 
      } 

当我打电话的观点,并与ID http://localhost:65001/Customer/Edit/1001,我得到的所有信息。但是,当我编辑它并发布编辑的表单时,我得到资源未找到错误。请帮忙。这里下面的型号:

  public class CustomerModelDTO 
       {   

        public int CustomerCode { get; set; } 

        public string FirstName { get; set; } 

        public string MiddleName { get; set; } 

        public string LastName { get; set; } 
        public double Telephone { get; set; } 
        public double Mobile { get; set; } 

        public string Email { get; set; } 
       } 
+0

请求是否到达编辑操作?你是否尝试在动作中创建断点? – Sriram

+0

发布时未达到编辑操作。 – user3647451

回答

0

我知道它的工作。这里是变化。

  [HttpPost("{customerCode}")] 
      public IActionResult Edit(int customerCode, CustomerModelDTO data)