ASP.NET MVC, passing Model from View to Controller

I’m having trouble with ASP.NET MVC and passing data from View to Controller. I have a model like this:

 public class InputModel {
   public List<Process> axProc { get; set; }

   public string ToJson() {
     return new JavaScriptSerializer().Serialize(this);
   }
 }

 public class Process {
   public string name { get; set; }
   public string value { get; set; }
 }

I create this InputModel in my Controller and pass it to the View:

public ActionResult Input() {
  if (Session["InputModel"] == null)
    Session["InputModel"] = loadInputModel();
  return View(Session["InputModel"]);
}

In my Input.cshtml file I then have some code to generate the input form:

@model PROJ.Models.InputModel

@using(Html.BeginForm()) {
  foreach(PROJ.Models.Process p in Model.axProc){
    <input type="text" />
    @* @Html.TextBoxFor(?? => p.value) *@
  }
  <input type="submit" value="SEND" />
}

Now when I click on the submit button, I want to work with the data that was put into the textfields.

QUESTION 1: I have seen this @Html.TextBoxFor(), but I don’t really get this “stuff => otherstuff”. I concluded that the “otherstuff” should be the field where I want to have my data written to, in this case it would probably be “p.value”. But what is the “stuff” thing in front of the arrow?

Back in the Controller I then have a function for the POST with some debug:

[HttpPost]
public ActionResult Input(InputModel m) {
  DEBUG(m.ToJson());
  DEBUG("COUNT: " + m.axProc.Count);

  return View(m);
}

Here the Debug only shows something like:

{"axProc":[]}
COUNT: 0

So the returned Model I get is empty.

QUESTION 2: Am I doing something fundamentally wrong with this @using(Html.BeginForm())? Is this not the correct choice here? If so, how do I get my model filled with data back to the controller?
(I cannot use “@model List< Process >” here (because the example above is abbreviated, in the actual code there would be more stuff).)

I hope someone can fill me in with some of the details I’m overlooking.

MVC: Passing Data from controller to view

How i can achieve passing data from controller to view without any action on view. In my project , Event handler will raise an event in controller that will change the data of view in MVC. I was sear

MVC confusion passing data from view to model via controller

If a view is served from a controller is it ok to pass data generated in that view via post and pass it straight to a model or do I need go back through the controller that served the view and call th

MVC3 Razor Passing Model from View to Controller

I have a MVC3 application using Entity Framework as the Model layer. In the EmployeeController, I have: public ActionResult GetEmployeeEdit(String id) { // Get the desired Employee var model = GetEmp

Passing Model Object Data from View, to Controller, to Model?

I’m brand new to ASP.net MVC (About a week in), so there is still quite a bit of confusion… How do I go about passing the current views model into the controller so I can get at the model data? View

MVC4 Passing model from view to controller

I have a view with a model populated with data relating to booking a taxi. In the model is a list of quotations with time, price, vehicle type in it which I display a list of using a foreach. Each tim

Passing data from razor view to controller in mvc

I need to pass the data from view to controller in an mvc project. I’ve seen many examples with passing to controller using Form and having a submit button. Actually I have @foreach to populate data l

Passing parameters from view to controller in mvc4

I have a confusing issue with passing parameters from a view to controller in MVC 4. The problem is that I have created a view and I have created the following code : <% using (Html.BeginForm(Dis

Passing model from controller to view

I have an html/jsp form that I am serializing(using jquery) and passing to a spring controller via ajax. In fact it is a spring-mvc form that is bound to an attribute. My first question is: as I am pa

MVC3 (Razor) Passing Model Data from View to Controller

I’m new to ASP.Net MVC. I need to know if there is a way to pass the Model (or a part of it, i.e. thereafter a search query) data of a View (Razor Engine) to a Controller. To explain in a better way w

Two ways of passing model back from view to controller

I’m new in MVC and have a question of principle about the way how the model is passed back from the view to the controller. In the usual way the model-object comes from the controller, spreads its d

Answers

  1. In @Html.TextBoxFor(stuff => otherstuff) stuff is your View’s model, otherstuff is your model’s public member.
  2. Since in the View you want to render input elements for the model member of a collection type (List), you should first create a separate partial view for rendering a single item of that collection (Process). It would look something like this (name it Process.cshtml, for example, and place into the /Views/Shared folder):

    @model List<PROJ.Models.Process>
    
    @Html.TextBoxFor(model => p.value)
    

Then, your main View would look like this:

@model PROJ.Models.InputModel

@using(Html.BeginForm()) {
  foreach(PROJ.Models.Process p in Model.axProc){
    @Html.Partial("Process", p)
  }
  <input type="submit" value="SEND" />
}

Also, check that the loadInputModel() method actually returns something, e.g. not an empty list.

Change your view to some thing like this to properly bind the list on form submission.

@using(Html.BeginForm()) {
  for(int i=0;i<Model.axProc.Count;i++){
   <span>
    @Html.TextBoxFor(model => model.axProc[i].value)
</span>
  }
  <input type="submit" value="SEND" />
}