Reputation: 13367
How can I pass back a (binded) model from a view to a controller method that is not an [httppost]?
View:
@Html.EditorFor(model => model.Name)
@Html.DisplayFor(model => model.Model)
// Table displays more properties of model
<input type="button" title="GetStuff" value="Get Stuff" onclick="location.href='@Url.Action("GetStuff")'" />
Controller Method:
public ActionResult GetStuff(ViewModel Model /* Realize this is non-httppost but...how can I get the model back with this type of scenario */)
{
// Get the name from per the model of the main view if the user changed it
string theName = Model.Name;
Model2 m2 = new Model2(){name = theName};
return ("someView", Model2);
}
Upvotes: 0
Views: 1919
Reputation: 39807
Instead of writing your own query strings, simply wrap your EditorFor stuff in an using Html.BeginForm() statement, remove the Javascript from your button, and change the button to a submit type.
@using (Html.BeginForm("GetStuff", "Controller", FormMethod.Get)
{
@Html.EditorFor(model => model.Name)
@Html.DisplayFor(model => model.Model)
// Table displays more properties of model
<input type="submit" title="GetStuff" value="Get Stuff"/>
}
The FormMethod.Get will send a query string request of the form elements to the action/controller defined in the Html.BeginForm definition.
Upvotes: 2
Reputation: 5197
Since the way you pass data in GET requests is via the query string, you'd have to put all that data on the query string. Model binding may work, at least for simple models, but you'll have to explicity include all that data in the action. For example:
@Url.Action("GetStuff", new { Name = Model.Name, Model = Model.Model })
But if any of that data is complex, you'll have to specify each and every subproperty yourself... Perhaps you should just post? :)
Upvotes: 2