2017-08-23 148 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)