简体   繁体   中英

An object used as a model in a partial view creates another in the controller?

There is something weird going on in my app. It's not dammageable, but it's a curious behavior and I'm reaching out to you to understand what's happening.

I was working on some partial view based on a model, and it worked. I figured out I had to replace a lot of stuff with the correct input.

So here's a snippet of my old model:

public class SearchObjInfo
{
    public string m_ObjName { get; set; }
    public string m_ObjType { get; set; }
    public decimal? m_ObjNumber { get; set; }
    public string m_ObjSymbol { get; set; }
    public string m_ObjPower { get; set; }
}

And here's the same snippet with the new class I made to construct this partial view:

public class SearchObjInfoPartial
{
    public string m_ObjName { get; set; }
    public IEnumerable<SelectListItem> m_ObjType { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0}")]
    public int m_ObjNumber { get; set; }
    public IEnumerable<SelectListItem> m_ObjPower { get; set; }
    public IEnumerable<SelectListItem> m_ObjSymbol { get; set; }
}

Now the way the render is made is actually quite identical, and not. I used lots of stuff like these before:

<label>
    Text: Write a name, part of a name, or a word.
</label>
Object Name: @Html.TextBox("_objectName") <br/>
Object Number: <input type="number" min="0" max="9999" name="_objNumber" value="decimal" style="width: 70px"/><br/>
Type: @Html.DropDownList("_objType", "All") <br/>
Power: @Html.DropDownList("_objSymbol", "=") @Html.DropDownList("_objValue", String.Empty)<br/>

But now I render my partial view this way:

@model MyApp.Utilities.SearchObjInfoPartial

Object Name: @Html.TextBoxFor(item => item.m_ObjName, Model.m_ObjName, String.Empty) <br/>
Object Number: @Html.EditorFor(item => item.m_ObjNumber)<br />
Power: @Html.DropDownListFor(item => item.m_ObjPower, Model.m_ObjPower, String.Empty)      @Html.DropDownListFor(item => item.m_ObjSymbol, Model.m_ObjSymbol, String.Empty)
Type: @Html.DropDownListFor(item => item.m_ObjType, Model.m_ObjType, String.Empty) <br/>

Before rendering I deal with the SelectLists, no problems here.

Now here's where it gets interesting:

In my controllers I used to have methods receiving huge amounts of data (see here: How to deal with many possible values to make a query? )

But now I made something else. Without thinking, I tried to add the old search model in the controller method like this:

public ActionResult BrowseObjectList(SearchObjInfo searchObj, string _objName, (...))

And I just found out that it works even if the receiving object is not the same as the one used in my partial view model. How is that even possible? I mean, the proper fields will fill up and I can "safely" deal with my searchObj item, though I do not find this secure after all...

Thats what MVC framework does for you man.

Browser simply sends the form collection to server as Name Value Collection. As the request hits server, MVC framework will match the values with parameter in the Action method.

  • Form collection values are mapped to Model object properties. This is done by doing a match with Property Name and Name of the value in Form collection. Just check the client side code by view source, you can see that the input tags will have an attribute 'name' which matches with the property name of model.
  • QueryString values will also be mapped to parameters in Action method based on name.
  • Even you add a hidden field and specify a parameter with same name in action method...tada you will get the value of hidden field in that variable on post back

In your case though the model is different, its property name are same m_ObjName, m_ObjType, m_ObjNumber, m_ObjSymbol, m_ObjPower. So MVC do a match for you.

Try with different property name and see the results ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM