Reputation: 11
My question : If we already have made view -a strongly typed, then why do we need to return model object back from controller post method to view - MVC 4 asp.net ?
For example : I have calculator view :
@using (Html.BeginForm())
{
<p>Number One : @Html.TextBoxFor(m => m.numberOne)</p>
<p>Number Two : @Html.TextBoxFor(m => m.numberTwo)</p>
<input type="submit" value ="Addition" name="calculateOperation" />
<input type="submit" value ="Subtraction" name="calculateOperation" />
<input type="submit" value ="Multiplication" name="calculateOperation" />
<input type="submit" value ="Division" name="calculateOperation" />
}
@using (Html.BeginForm())
{
<p>Output : @Html.TextBoxFor(m => m.result)</p>
}
and controller :
public ActionResult Calculate(Calculator model, string calculateOperation)
{
if (calculateOperation.Equals("Addition"))
{
int[] array = { 1, 12, 5, 26, 7, 14, 3, 7, 2 };
model.result = model.numberOne + model.numberTwo;
}
if (calculateOperation.Equals("Subtraction"))
{
model.result = model.numberOne - model.numberTwo;
}
if (calculateOperation.Equals("Multiplication"))
{
model.result = model.numberOne * model.numberTwo;
}
if (calculateOperation.Equals("Division"))
{
model.result = model.numberOne / model.numberTwo;
}
return View(model);
}
If I don't return the model object, I don't get value of model.result.
Looking for a valid reason.
Upvotes: 1
Views: 1679
Reputation: 11945
Well, you don't have to send back a model, you could just use the FormCollection parameter, but then you would have to fetch the values and cast them to the correct type your self.
public ActionResult Calculate(FormCollection form, string calculateOperation)
{
// Need to check if form["numberOne"] is null or empty or do a int.TryParse()
int numberOne = int.Parse(form["numberOne"]);
}
With a strongly typed model you get that for free by the model binders in asp.net mvc. The code looks much cleaner and it's easier to use.
With a model you also get the power of attributes, like validation and scaffolding. It's much cleaner and easier to validate most scenarios using a model with validation attributes.
In this case you need to send a model to the view simply because the view requires it. That's how it is designed. How would the model or the view know that you have made an calculation if you don't store it somewhere? Of course you could also use ViewBag:
ViewBag["result"] = model.numberOne + model.numberTwo;
And in your view:
<p>Output :@Html.TextBox("result", (string)ViewBag["result"])</p>
Upvotes: 1
Reputation: 1212
HTTP is a stateless protocol. Hence, when you do work on the server, if you want it to display something on the client, you have to send it back down. An MVC strongly-typed view is really just an abstraction on top of the rendering engine.
When you "Submit" the form you are just doing an HTTP POST back to your controller action (an http request).
Calling
return View(model)
means that you are sending an HTTP response that returns a rendered html page (using your view). In your case you're simply passing in the model as a parameter.
Upvotes: 1
Reputation: 1563
There's no actual requirement for your controllers method to return anything that consumes that or any other model. As a result you still need to be explicit with what View and the data associated with it that you want to return.
They could add some sort of overload to View that would implicitly assume it should use some ViewModel in the method parameters, but that's non-intuitive and unnecessary.
Upvotes: 0
Reputation: 7468
I always figured that this was to cover cases when there was some kind of explanation or response type data.
For example. You submit an address to be added to the database and you have a service which checks the address for correctness. If it is correct, it gets persisted, otherwise it gets corrected, added to a special field in the original object and sent back for confirmation.
Upvotes: 0