2017-04-23 63 views
0

我在ASP.NET的MVC 4的應用程序如下:郵政形式總是返回null

1.ProductsController.cs

namespace MvcApplication2.Controllers 
{ 
    public class ProductsController : Controller 
    { 
     // 
     // GET: /Products/ 

     [HttpGet] 
     public ActionResult Products() 
     { 
      List<Product> prList = new List<Product>(); 
      Product p1 = new Product(); 
      p1.ProductName = "J & J"; 
      p1.Price = 40; 
      p1.Ratings = 5; 
      prList.Add(p1); 
      Product p2 = new Product(); 
      p2.ProductName = "Himalaya"; 
      p2.Price = 20; 
      p2.Ratings = 2; 
      prList.Add(p2); 
      return View(prList); 

     } 

     [HttpPost] 
     public ActionResult Products(FormCollection prList,List<MvcApplication2.Models.Product> fg) 
     { 
      return View(prList); 
     } 

    } 
} 

2. ProductList.cs

namespace MvcApplication2.Models 
{ 


    public class Product 
    { 
     public string ProductName { get; set; } 
     public int Price { get; set; } 
     public int Ratings { get; set; } 
    } 

} 

3. Products.cshtml

@{ 
    Layout = null; 

} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Products</title> 
    @Styles.Render("~/Content/css") 
     @Scripts.Render("~/bundles/modernizr") 

    <script src="~/Scripts/jquery-3.2.1.min.js"></script> 
</head> 
@model IEnumerable<MvcApplication2.Models.Product> 
@using (@Html.BeginForm("Products", "Products", FormMethod.Post)) 
{ 
<body> 
    <div style="width:100%;height:100%;position: relative "> 
     <div style="width:100%;top:0px;height:40px;position:relative;background-color:purple"> 
      <input type="submit" value="Sort price" style="float : right;width:30px;" id="SearchId" /> 
      @Html.TextBox("Search Box", null, new { @style = "float:right;width:80px "}); 
      <input type="submit" value="submit" /> 
     </div> 
     <div id="tableDiv"> 
      <table id="tableId"> 
       <tr> 
        <th>Name</th> 
        <th>Price in Rs.</th> 
        <th>Ratings</th> 
       </tr> 
@foreach (var drawing in Model) 
{ 

       <tr> 
        <td>@drawing.ProductName</td> 
        <td>@drawing.Price</td> 
        <td>@drawing.Ratings</td> 
       </tr> 

} 
      </table> 
     </div> 
    </div> 
</body> 
} 
</html> 

每當我瀏覽到http://localhost:5858/Products/Products,點擊和提交的控逆變談到在產品的方法[HttpPost],但該模型始終是空的。

這是什麼,我在這裏失蹤?我期待在頁面加載時返回相同的模型,爲什麼模型變空了?

+2

你只是張貼'Search_Box' ,所以沒有什麼可以綁定的。使用'@ Html.EditorFor(m => ...)'。 – CodeCaster

回答

1

的模型是空的,因爲你的形式不包含除搜索框以外的任何輸入元素:

@Html.TextBox("Search Box", null, new { @style = "float:right;width:80px "}) 

這樣被髮送到服務器的唯一的事情就是在這個搜索框中輸入的值。您無法期望在您的發佈操作中獲得List<Product>。由於是不應該這樣的信息由客戶端進行修改,所有你需要做的就是在你的POST操作來獲取這個名單你在你的GET動作做的方式一樣:

[HttpGet] 
public ActionResult Products() 
{ 
    var prList = this.GetProducts(); 
    return View(prList); 
} 

[HttpPost] 
public ActionResult Products(FormCollection fc) 
{ 
    var prList = this.GetProducts(); 

    // TODO: based on the search parameter sent from the client 
    // here you probably want to filter the prList before passing it 
    // back to the view 

    return View(prList); 
} 

private List<Product> GetProducts() 
{ 
    List<Product> prList = new List<Product>(); 
    Product p1 = new Product(); 
    p1.ProductName = "J & J"; 
    p1.Price = 40; 
    p1.Ratings = 5; 
    prList.Add(p1); 
    Product p2 = new Product(); 
    p2.ProductName = "Himalaya"; 
    p2.Price = 20; 
    p2.Ratings = 2; 
    prList.Add(p2); 
    return prList; 
}