帖子表单总是返回null

时间:2017-04-23 13:59:09

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

我在asp .net mvc 4中有一个应用程序如下:

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 并单击并提交时,控制器会在Products方法中显示[HttpPost],但模型始终为空。

我在这里缺少什么?我希望在加载页面时返回相同的模型,为什么模型变空?

1 个答案:

答案 0 :(得分: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;
}
相关问题