Shaitender Singh
Shaitender Singh

Reputation: 2207

Null value in Model Class property in MVC 3

I have a question. I need to save all values of my model. Class

public class AssessmentAreaContent
{
  //SysAssessmentSubAreaCategory is enity model class
  public List<SysAssessmentSubAreaCategory> ListSysAssessmentSubAreaCategory;   
}

Controller

public ActionResult AssessmentArea(int id)
            { 
              var model = new AssessmentAreaContent();
                model.ListSysAssessmentSubAreaCategory = db.SysAssessmentSubAreaCategory.Where(c => c.AssessmentAreaId == id).ToList();
                return View(model);
            } 

Controller Http Post Method

 [HttpPost]
        public ActionResult AssessmentArea(Web.Models.AssessmentAreaContent assessmentAreaContent)
        {

            //assessmentAreaContent.ListSysAssessmentSubAreaCategory is null here ?? ; 
            return RedirectToAction("AssessmentArea", "AssessmentArea");
        }

View

@model Web.Models.AssessmentAreaContent

@{
    ViewBag.Title = "Area";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
    @{ 

    if (Model != null)
                        {
       foreach (var category in Model.ListSysAssessmentSubAreaCategory)
                            {           


              <strong>@Html.EditorFor(item => category.AreaCategoryName)</strong>  
                <br />
       }
}
}
<input type="submit" name="button" class="btn" value="Save" />
}

I m getting Null value in assessmentAreaContent.ListSysAssessmentSubAreaCategory; Im using Entity Framework with MVC 3 How can I do that please ?

Thanks

Upvotes: 0

Views: 1747

Answers (2)

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

make an editor template with the name SysAssessmentSubAreaCategory. note that name of template is same as type of your list objects. in this template write something like

@model Web.Models.SysAssessmentSubAreaCategory
<strong>@Html.EditorFor(item => category.AreaCategoryName)</strong>  
                <br />

in your main view just make these changes

if (Model != null)
                        {
       Html.EditorFor(x=>x.ListSysAssessmentSubAreaCategory)
       }

leave the rest unchanged. fill out the form and post it and you will have values in the controller.

Upvotes: 1

Jahan Zinedine
Jahan Zinedine

Reputation: 14864

There is no input holding values for ListSysAssessmentSubAreaCategory so that in post back you have that property filled.

You need that values to be saved server side (e.g. in session ) or be represented in input form.

Upvotes: 0

Related Questions