Asp.Net MVC2 Model Binding Problem.

Posted by Pino on Stack Overflow See other posts from Stack Overflow or by Pino
Published on 2010-05-19T09:34:26Z Indexed on 2010/05/19 10:30 UTC
Read the original article Hit count: 296

Filed under:
|

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

Using the following,

<%@ 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();
        }

    }
}

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc