2017-04-23 1 views
0

J'ai une application dans asp .net 4 mvc comme suit:forme toujours après le retour 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. Produits.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> 

Chaque fois que je navigue à http://localhost:5858/Products/Products et cliquez sur soumettre et, le contôle vient [HttpPost] dans les méthodes produits, mais le modèle est toujours vide.

Qu'est-ce qui me manque ici? Je m'attends à ce que le même modèle soit retourné quand la page a été chargée, pourquoi est-ce que le modèle devient vide?

+2

Vous publiez uniquement 'Search_Box' , donc il n'y a rien à lier. Utilisez '@ Html.EditorFor (m => ...)'. – CodeCaster

Répondre

1

Le modèle est vide parce que votre formulaire ne contient aucun élément d'entrée autre que la zone de recherche:

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

La seule chose qui est envoyé au serveur est la valeur entrée dans ce champ de recherche. Vous ne pouvez pas vous attendre à obtenir un List<Product> dans votre action Publier. Étant donné que ces informations ne sont pas censé être modifié par le client tout ce que vous avez à faire est de récupérer cette liste dans votre action POST de la même façon que vous avez fait dans votre action 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; 
}