ASP.NET MVC2 - specific fields in form pass via a specific object?

亡梦爱人 提交于 2020-01-15 09:41:30

问题


In database I have Contacts table:

ContactID (int)
FirstName (varchar)
LastName (varchar)
...
XmlFields (xml) // This field is xml type

To create a new contact, I created two classes - one for regular fields and other to display fields from XmlFields field.

In Controller, I have following:

public ActionResult Create(Contact contact, FormCollection collection)
...

Regular field I catch with contact object and those that need to be stored as xml in XmlFields I try to catch with collection object. Problem is that collection object catches all fields, so I wonder if it is possible to isolate xml fields when posting to a specific object so that I can easily manipulate with them. I need this in separated objects because these xml fields are going to be generated dynamically and will be different for every user.

Thanks in advance,
Ile


回答1:


You can separate fields like:

public ActionResult Create(int ContactID, string FirstName)

And pass the raw data to the XML... There isn't any automatic way for MVC to know where to push what data to, unless you consider creating a custom model binder: http://davidhayden.com/blog/dave/archive/2008/09/08/CustomModelBinderMoreUIValidationASPNETMVC.aspx which is another valid option.

HTH.



来源:https://stackoverflow.com/questions/2744054/asp-net-mvc2-specific-fields-in-form-pass-via-a-specific-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!