ASP.NET MVC2 Model Binding Issue

Why is my controller getting 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 next 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;
}

      

+2


a source to share


1 answer


EDIT: you need to use properties, not variables

[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }

public string LongDesc { get; set; }

public string ShortDesc { get; set; }

      

Also...

You are not passing the model back to the view.

return View(NewProduct);

      



I usually pass an empty model to a GET action.

ProductModel NewProduct = new ProductModel();
return View(NewProduct);

      

Thus, if you want to set any defaults, you can do it easily.

Sample Code in Full I also added try and catch blocks around product addition and sample views that you could return with success or failure:

[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 do stuff.  
              var ProductServ = new ProductService();  
              ProductServ.AddProduct(NewProduct); 

    }
    catch(Exception){
           return View("Fail");
    }

     return View("Success");  
} 

      

+4


a source







All Articles