0

Please i have a model class below

 public class EditJob
 {
        public JobPost JobPost { get; set; }
        public IEnumerable<JobRequirement> JobRequirement { get; set; }
        public IEnumerable<JobResponsibility> JobResponsibility { get; set; }
  }

I strongly bind the class to a view and want to use it for editing in controller. I have a controller which receives model like below.

[HttpPost]
public ViewResult SaveJob(EditJob model)
{

}

I Dynamically bind my Ienumerable to texboxes in my view like below.

@for (int i = 0; i < Model.JobResponsibility.Count();i++ )
 {

  JobWebsite.Domain.Enitities.JobResponsibility rs = Model.JobResponsibility.ElementAt(i);
  <label class="control-label">Responsibility :</label>
   Html.Hidden("Responsibility[" + i + "]", rs);
   @Html.TextBoxFor(x => rs, new { @class = "form-control" }) <br />

 }

On my button click, I realized the two IEnumerable methods from my model are not binding . The JobPosts in the model is not empty, yet my Ienumerable properties not always empty . I added a hidden field holding my values but i am having problem passing them to the controller. Please how do i achieve this ?

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116

2 Answers2

1

If you inspect you html, you will see that the controls name attributes do not have the correct indexers for binding. Either change the collections to IList and use as follows: (note you have not posted the model for JobResponsibility so I'm assuming it contains properties ID and Name)

@for (int i = 0; i < Model.JobResponsibility.Count; i++ )
{
  @Html.HiddenFor(m => m.JobResponsibility[i].ID)
  @Html.TextBoxFor(m => m.JobResponsibility[i].Name)
}

Alternatively, create a custom EditorTemplate for type of JobResponsibility

/Views/Shared/EditorTemplates/JobResponsibility.cshtml

@model JobResponsibility
@Html.HiddenFor(m => m.ID)
@Html.TextBoxFor(m => m.Name)

and in the main view

@model EditJob
@using(Html.BeginForm())
{
  .....
  @Html.EditorFor(m => m.JobResponsibility)
  ....
}
  • I tried the first one it still null. I wanted to try the Editor Template, but it returns only the id. 123456. My id field is hidden yet it displayes 123456. I am confused – Nuru Salihu Mar 06 '15 at 05:18
  • Sorry I spelled the folder name wrond thats why. The Editor Template works. – Nuru Salihu Mar 06 '15 at 05:21
  • Both options I gave work, so there is something else wrong in your code if the first one is not working. They both should produce identical output for the `name` attributes - for example `` and `` etc. –  Mar 06 '15 at 05:24
  • @thank you for your time sir. I am redefining my view to use the editortemplates. Since it works with that , i think i am satisfied . Also i agree with you . The first should work as well. but `JobResponsibility[0]` ? my jobResponsiblity is IEnumerable. Or do you mean `JobResponsibility.ElementAt(i)` ? . Anyway i try the later and it returns null. And i tried JobResponsibility[0] and it gives error. Thank you sir. – Nuru Salihu Mar 06 '15 at 07:32
  • As I noted in the first paragraph, your need to make the model `IList` - i.e `public List JobResponsibility { get; set; }` otherwise you can't use a `for` loop, but in any case I prefer the `EditorTemplate` approach anyway so stick with it :) –  Mar 06 '15 at 07:35
  • Just curious sir . But do you have any suggestion on how i can generate my textboxes dynamically? or tu link perhaps?. As u may noticed i am using this template for edit. However, i want to use them for creating new post . In that case, the use should be add to add the number of responsibilities or reuirements necessary . Do you have any suggestion ? javascript, Jquery or mvc any link or suggestion would be appreciated sir. :) – Nuru Salihu Mar 06 '15 at 07:54
  • [My answer here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) shows 2 options. –  Mar 06 '15 at 08:03
  • Thank you four time. Will go through. – Nuru Salihu Mar 06 '15 at 08:18
  • hmmm. I couldn't get it going sir. I am trying to make it simple . I want to display empty 12 textboxes for requirement and twelve for responsiblities on the page. I want to bind the empty textboxes to my Ienumerable which is empty already. because its empty, It isn't binding sir. I am having serious difficulty with this. Unless i change my Controller or created another . Is there any way i can add and bind my textboxes to my empty ienumerable sir? Important , not dynamically, just looping through . – Nuru Salihu Mar 07 '15 at 20:07
  • Its my first mvc project and really wanna make it successful. :) – Nuru Salihu Mar 07 '15 at 20:08
  • If you want 12 empty textboxes, then you need to populate your collection with 12 items in the controller before you pass the model to the view (you can bind to something that does not exist). But unless you are always expecting the user to enter exactly 12 values, then use the techniques described in the link I noted above. –  Mar 07 '15 at 22:45
0

I can't see anything at the moment binding to JobResponsibility. Try changing

Html.Hidden("Responsibility[" + i + "]", rs);

to

Html.Hidden("JobResponsibility[" + i + "]", rs);
dav_i
  • 27,509
  • 17
  • 104
  • 136