views:

61

answers:

1

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;
}
+2  A: 

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 normally pass a blank model in to the GET action too

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

This way if you wish to set any default values you can do so easily.

Code example in full I've also added try and catch blocks around the adding of the product and given example views you could be returning on success or fail:

[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");  
} 
Andi
@SocialAddict I'vre tried that! The ModelState.IsValid always returns true, it then trys to save and I get errors because the model does not contain the values that were entered into the textbox. Somthings not right in the populate of the model bind.
Pino
Can you post the model. Do you have validation in that?
Andi
@SocialAddict. One of them days.
Pino