0

I'm editing a code written by someone who left nearly no comments. I'm trying to find where does a parameter in a httppost function come from. (ism in this case).

 [HttpPost]
 [SessionCheckFilter]
 [LogFilter]
 [ValidateInput(false)]
 [ActionName("Save")]
 public ActionResult Save(StructureViewModelPOST ism){...}

The thing is, StructureViewModelPOST is only used in this function. There are no other references to it. I want to change the data in the ism, but I have no idea where it comes from. This function is called when someone presses the "save" or "save and close" button inside my form

<a class="btn btn-primary fixed-width navigation"  href="#" onclick="checkTotalValue(1);"><i class="icon-save pull-left navigation"></i>Save</a>

And the form is created like this inside my view:

@using (Html.BeginForm("Save", "Invoice", FormMethod.Post, new { @class = "form-horizontal" }))

I realise i'm not providing you with all the information necessary to answer this quesion, but I can't just paste the whole project and I don't really know what information IS needed to answer it.

Xyzk
  • 1,332
  • 2
  • 21
  • 36
  • 1
    The parameter "ism" should be the form that is posted from the view. In this case it will be of the type StructureViewModelPOST. – Wheels73 Jun 22 '17 at 09:29
  • 1
    From your form controls, from route values, from query string values, from JSON data if using an ajax post with the appropriate `contentType`. All of them send name/value pairs and it the name matches one of your property names, the `DefaultModelBinder` will bind it using various `ValueProviders` –  Jun 22 '17 at 09:39

2 Answers2

2

The MVC modelbinder works by convention, that is, it tries to match the name and id of a HTML <input name="ThePropertyName" id="ThePropertyName"/> element to a property in the model class accepted by the post action in the controller.

So if StructureViewModelPOST class has a property string ThePropertyName { get; set; }, this property will be filled with the value of the matching input that has the name="ThePropertyName".

There is more than one way to render this input with the correct name.

Using a ViewModel defined with the @model directive, and resolving the input type to be used with an editor template:

@model StructureViewModelPOST
@Html.EditorFor(m => m.ThePropertyName)

Or more directly:

@Html.TextBox(nameof(StructureViewModelPOST.ThePropertyName), "")
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
1

Basically action parameters is bound from posted form fields

each Field Is Bound To The Corresponding Properties With The Same Name

example :

<input type='text' name='TotalDiscount' />

will be bound to ism.TotalDiscount

Check These Links

What is model binding in ASP.NET MVC?

and Model Binder Section Here

https://weblogs.asp.net/scottgu/asp-net-mvc-preview-5-and-form-posting-scenarios

Hasan Elsherbiny
  • 598
  • 2
  • 14
  • 31