ASP.NET MVC2 model binding problem
صفحه اصلي

ASP.NET MVC2 model binding problem



Why is my controller receiving an empty model in this case?

Using

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<X.Models.ProductModel>" %>      <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">          <h2>Product</h2>          <% using (Html.BeginForm() {%>              <%: Html.ValidationSummary(true) %>                  <div class="editor-label">                     Product Name                 </div>                 <div class="editor-field">                     <%: Html.TextBoxFor(model => model.Name) %>                     <%: Html.ValidationMessageFor(model => model.Name) %>                 </div>                  <br />                  <div class="editor-label">                     Short Description                 </div>                 <div class="editor-field">                     <%: Html.TextAreaFor(model => model.ShortDesc) %>                     <%: Html.ValidationMessageFor(model => model.ShortDesc) %>                 </div>                  <br />                  <div class="editor-label">                     Long Description                 </div>                 <div class="editor-field">                     <%: Html.TextAreaFor(model => model.LongDesc) %>                     <%: Html.ValidationMessageFor(model => model.LongDesc) %>                 </div>                  <p>                     <input type="submit" value="Create" />                 </p>          <% } %>      </asp:Content> 

and the following controller.

using System.Web.Mvc; using X.Lib.Services; using X.Models;  namespace X.Admin.Controllers {     public class ProductController : Controller     {          [HttpGet]         public ActionResult ProductData()         {             return View();         }          [HttpPost]         public ActionResult ProductData(ProductModel NewProduct)         {             //Validate and save             if(ModelState.IsValid)             {                 //Save And do stuff.                 var ProductServ = new ProductService();                 ProductServ.AddProduct(NewProduct);             }              return View();         }      } } 

Model:

public class ProductModel {     public int ID;      [Required(ErrorMessage = "Name is required")]     public string Name;      public string LongDesc;     public string ShortDesc; } 

Failing to connect to Database due to database in recovery

1:

Partial view postback from a standard Html form with MVC
EDIT: you need to use properties not variables. Using a .NET MVC Controller Action as the Source for an HTML <img>
[Required(ErrorMessage = "Name is required")] public string Name { receive ; set; }  public string LongDesc { receive ; set; }  public string ShortDesc { receive ; set; } 
Also.... ASP.NET MVC: How do I validate a model wrapped in a ViewModel? You are not passing the model back to the view.. ASP.NET MVC Rich Text Editor not showing buttons
return View(NewProduct); 
I normally pass a blank model in to the GET action too. Is there a template language for ASP.NET MVC like JSTL and OGNL?
ProductModel NewProduct = new ProductModel(); return View(NewProduct); 
This way if you wish to set any default values you must did so easily.. Selling upper management on converting to ASP.NET from Classic ASP Code case in full I've also added try and catch blocks around the adding of the product and given case views you could be returning on success or fail:. Membership provider stopped working on using Membership Provider in ASP.Net MVC
[HttpGet]   public ActionResult ProductData()   {        ProductModel NewProduct = new ProductModel();     return View(NewProduct);   }    [HttpPost]   public ActionResult ProductData(ProductModel NewProduct)   {        //Validate and save        if(!ModelState.IsValid)        {        // Return the model back to view     return View(NewProduct);       }        try{         //Save And did  stuff.                 var ProductServ = new ProductService();                 ProductServ.AddProduct(NewProduct);       }     catch(Exception){            return View("Fail");     }       return View("Success");   }  


79 out of 100 based on 74 user ratings 189 reviews

^