2

I have a list of class object and which is bind with view like this

@model List<Rep.Models.ContactReportSettingViewModel>
var accountArr = Model.Select(x => new { x.AccountId, x.CarrierId, x.AccountNumber, x.CarrierName, x.ClientId, x.ContactId }).Distinct();

I have a loop here on var object

@foreach (var accountRow in accountArr)
{
  @Html.LabelFor(x => accountRow.AccountNumber, accountRow.AccountNumber, new { @id = accountRow.AccountId })              

but when I click on save it is returning null or values or not set with the class properties I am accessing this in controller like this:

public RESULT method(List<ContactReportSettingViewModel> model)
{ 
  model is null here
  // return View(model);            
}

But in model I am getting null. What I am doing wrong?

When I use this

public RESULT method(ContactReportSettingViewModel model)
{ 
  // return View(model);            
}

Then in model object I can see all the properties but values does not set to those properties

15
  • 4
    Please post all of your code, including the View. Commented Aug 25, 2015 at 11:25
  • Its public ActionResult method(List<class> model) but if its not binding its because you view is wrong. Post your code. Commented Aug 25, 2015 at 11:26
  • Make sure you have [HttpPost] decorated over the action method Commented Aug 25, 2015 at 11:28
  • How you binded model with view? I mean the exact statement on top of your view? Commented Aug 25, 2015 at 11:30
  • Show the action/method responsible for creating the View. Are you not sending a null model to the view. You have to instantiate your model maybe: public ActionResult Index() { var model = new yourModel(); Return View(model); } Commented Aug 25, 2015 at 11:36

2 Answers 2

2

You cannot use a foreach loop to generate form controls for a collection because your generating duplicate name attributes that have no relationship to your model (and duplicate id attributes which is invalid html). You can use either a for loop in the view, or use an EditorTemplate for your model.

Note you need to remove your Linq .Select() code and do the filtering in the controllers GET method.

Using a for loop in the main view (note the model must be IList<T>)

@model List<Rep.Models.ContactReportSettingViewModel>
@using (Html.BeginForm())
{
  @for (int i = 0; i < Model.Count; i++)
  {
    @Html.LabelFor(m => m[i].AccountNumber)
    @Html.TextBoxFor(m => m[i].AccountNumber)
    @Html.ValidationMessageFor(m => m[i].AccountNumber)
    .....
  }
  <input type="submit" .../>
}

Using an EditorTemplate. Create a partial view in /Views/Shared/EditorTemplates/ContactReportSettingViewModel.cshtml (note the name of the file must match the model class name)

@model Rep.Models.ContactReportSettingViewModel

@Html.LabelFor(m => m.AccountNumber)
@Html.TextBoxFor(m => m.AccountNumber)
@Html.ValidationMessageFor(m => m.AccountNumber)
  .....

and then in the main view (note the model can be IEnumerable<T>)

@model IEnumerable<Rep.Models.ContactReportSettingViewModel>
@using (Html.BeginForm())
{
  @Html.EditorFor(m => m)
  <input type="submit" .../>
}

In both cases the generated html will include the correct name attributes with indexers which will be bound to your model in the POST method

<input type="text" name="[0].AccountNumber" .. />
<input type="text" name="[1].AccountNumber" .. />
Sign up to request clarification or add additional context in comments.

Comments

0

Follow the following checklist

1) Make sure you added thing binding statement on top of your view

@model List<ClassName>

2) Then check your is being submitted to the function your mentioned in question and also check the parameter type is same as you mentioned while binding the page.

if you are using html table type structure to display list items then you also need to bind your list with each row. like for Cell[0][0] bind yourList[0].EmployeeId, Cell[0][1] bind yourList[0].EmployeeName and so on for all the column and then rows by using loop.

1 Comment

I have added that and because of that I am able to see the list of records but the problem is in the saving only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.