2017-06-16 64 views
0

好吧,现在我正在尝试学习.net core mcv,并且我有一个将数据从MySQL映射回我的表单的问题。当我使用单个按钮将表单作为单个文本框以及表单之外的其他字段(但在同一页上)时,映射工作正常。如果我包含表单中的所有字段,则会获取数据,但不会显示在页面上。我甚至已经将多个提交按钮之一编码为数据的更新。我使用第一个文本框从数据库中获取项目(它不会映射到文本框),然后在第二个文本框(应该有现有数据但是为空)中放入要在数据库中更新的信息,单击该文本框的提交按钮,并更新数据库(但视图中的文本框保持空白)。Multibutton form不会将数据映射回文本框.net core 1.1

我的模型:

using System; 

namespace DbTest.Models 
{ 
public class ProductInventory 
    { 
     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
     public string Field3 { get; set; } 
     public int Field4 { get; set; } 
    } 
} 

我的控制器:

using System; 
using Microsoft.AspNetCore.Mvc; 
using MySql.Data.MySqlClient; 
using Microsoft.AspNetCore.Authorization; 

using DbTest.Models; 

namespace DbTest.Controllers 
{ 
    public class InventoryController : Controller 
    { 
//  [Authorize] 
     public IActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public IActionResult ProcessForm(string button, ProductInventory p) 
     { 
      IActionResult toDo = null; 

      if (button == "Button1") 
      { 
       toDo = GetItem(p); 
      } 
      if (button == "Button2") 
      { 
       toDo = UpdateField2(p); 
      } 
      if (button == "Button3") 
      { 
       toDo = UpdateField3(p); 
      } 
      if (button == "Button4") 
      { 
       toDo = UpdateField4(p); 
      } 

      return toDo; 
     } 

//  [HttpPost] 
     public IActionResult GetItem(ProductInventory p) 
     { 
    //CODE SNIP - DATABASE QUERY, IT ALL WORKS, SO WHY BOTHER YOU WITH THE DETAILS? 
     return View("Index", p); 
     } 

     public IActionResult UpdateField2(ProductInventory p) 
     { 
     //CODE SNIP - DATABASE UPDATE, ALL WORKS, NOTHING TO SEE HERE 
     return View("Index", p); 
     } 
    } 
} 

最后,我的观点:

@model DbTest.Models.ProductInventory 

@{ 
    ViewData["Title"] = "Inventory Page"; 
} 

@using (Html.BeginForm("ProcessForm", "Inventory", FormMethod.Post)) 
{ 
     <div> 
      Search Item (Field 1): 
      @Html.TextBoxFor(model => model.Field1) 
      <input type="submit" name="button" value="Button1" /> 
     </div> 
     <div> 
      Field 2: 
      @Html.TextBoxFor(model => model.Field2) 
      <input type="submit" name="button" value="Button2" /> 
     </div> 
     <div> 
      Field 3: 
      @Html.TextBoxFor(model => model.Field3) 
      <input type="submit" name="button" value="Button3" /> 
     </div> 
     <div> 
      Field 4: 
      @Html.TextBoxFor(model => model.Field4) 
      <input type="submit" name="button" value="Button4" /> 
     </div> 
} 

再次重申,如果我Button1的后关闭窗体:

@using (Html.BeginForm("ProcessForm", "Inventory", FormMethod.Post)) 
{ 
     <div> 
      Search Item (Field 1): 
      @Html.TextBoxFor(model => model.Field1) 
      <input type="submit" name="button" value="Button1" /> 
     </div> 
} 
     <div> 
      Field 2: 
//etc. 

映射工作,但只有表单的第一个字段和按钮工作。使用围绕所有四个字段和按钮的表单,映射不起作用,但第二个按钮的编码DOES会在单击Button2时更新数据库。

有人可以解释我在这里做错了吗?

谢谢!

回答

0

首先,不要在ASP.NET Core中使用html助手。他们的工作原理并不是最佳实践。尽可能使用tag helpers。此外,不要将您的db模型用作视图模型。

关于您的Index操作:您忘记将视图模型传递给您的视图。

在您的ProcessForm操作中,您实例化了IActionResult,然后为其分配一个(操作)功能。不要这样做。请使用return RedirectToAction("ActionName");。 在你的情况下,我会处理ProcessForm操作或函数中的数据库更新,该函数不返回IActionResult

总而言之,我只能推荐你阅读ASP.NET Core文档,然后再问你是否还没有得到它的工作。我建议你从阅读this开始。