0

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

1
  • Monu ur model is a plain object not an enumeration how do u manage to iterate over it? Commented Jan 7, 2012 at 7:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Monu u r welcome. analyze the generated html and read this article haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx and you would know why this time list bound automatically
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.