简体   繁体   English

在ASP.NET MVC 3中发布动态表单

[英]Posting Dynamic Forms in ASP.NET MVC 3

On one of the forms I am working on, if the user selects DropDownList "Type", based on the value selected, I would show Dynamically a set of other DropDownLists. 在我正在处理的其中一种表单上,如果用户根据所选值选择DropDownList“Type”,我将动态显示一组其他DropDownLists。

For instance, Types: Car, Apartment Selecting "Car", would dynamically add two new DropDownLists "Price" & "Model". 例如,类型:汽车,公寓选择“汽车”,将动态添加两个新的DropDownLists“价格”和“模型”。 Selecting "Building", would dynamically add two new DropDownLists "Area" & "Floor" 选择“Building”,会动态添加两个新的DropDownLists“Area”和“Floor”

My question is how to handle the posted data to server? 我的问题是如何处理发布到服务器的数据? I would typically receive the form's fields and they get "model bound" automatically. 我通常会收到表单的字段,并自动获得“模型绑定”。 But what about those dynamic fields that were added on the page based on user's selection? 但是根据用户的选择在页面上添加的那些动态字段呢? How will I retrieve them inside the Action? 我将如何在Action中检索它们?

Thanks 谢谢

You can use the FormCollection to access all the fields on the page. 您可以使用FormCollection访问页面上的所有字段。 However you'll lose the automatic model binding and will have to iterate through the form collection to find your values as follows: 但是,您将丢失自动模型绑定,并且必须遍历表单集合以查找您的值,如下所示:

public ActionResult ActionMethod(FormCollection formCollection)
{
    foreach (var key in formCollection.AllKeys)
    {
        var value = formCollection[key];
    }
}

Of course you could also just include the dynamic dropdowns in your model. 当然,您也可以在模型中包含动态下拉列表。

Ah misunderstood this. 啊误解了这一点。

If you know what Dropdowns can potentially be added, I would always just have a value on your model. 如果你知道可以添加什么下拉列表,我总是会在你的模型上有一个值。 MVC will set them as a default value if nothing is received. 如果没有收到任何内容,MVC会将它们设置为默认值。

EDIT: You can still access the forms collection in a more raw way from withing controllers using 编辑:您仍然可以使用控制器以更原始的方式访问表单集合

Request.Form.Keys
Request.QueryString.Keys

Request["ExpectedDropdownName"]

Of course these will be all values posted by your form so you will need a way to recognise one of your dropdowns like a prefix or something. 当然,这些将是您的表单发布的所有值,因此您需要一种方法来识别您的一个下拉菜单,如前缀或其他内容。 For example 例如

   foreach (var key in Request.Form.AllKeys.Where(k => k.StartsWith("dynamic-dropdown-"))
   {
       var selectedValue = Request[key];
   }

I still don't really understand how you intend to process the dynamic dropdowns if you don't know what they will be and this can cause some issues and will make validation entirely dynamic and client side (not 100% safe). 如果您不知道它们将会是什么,我仍然不明白您打算如何处理动态下拉列表,这可能会导致一些问题并使验证完全动态和客户端(不是100%安全)。 That could present some security issues as well, but there are some sceanarios where you might use this and I'll assume you have one of them. 这也可能会带来一些安全问题,但是有一些情况你可以使用它,我会假设你有其中一个。

If that's not the case, don't forget that just because a Model has a property, it doesn't have to be posted back at all. 如果情况并非如此,请不要忘记,因为模型具有属性,所以根本不需要回发。

You could do: 你可以这样做:

public class MyModel
{
    [Required]
    public string FirstName { get; set; }

    public string PossibleDropdown1 { get; set; }

    public string PossibleDropdown2 { get; set; }
}

The controller will do it's best to populate the model, but if you don't pass some fields back from the form, they will just be null and the action will still work. 控制器最好填充模型,但是如果你没有从表单中传回一些字段,它们将只是null并且操作仍然有效。

I have also implemented much more complex scenarios like this using objects that are children of the parent model and all nullable. 我还使用父模型的子对象和所有可空的对象实现了更复杂的场景。 That requires a bit of fancy work around ajax calls to EditorTemplates in mock Views to ensure the prefixing is right for MVC to parse, but I won't go into that here. 这需要在模拟视图中围绕对EditorTemplates的ajax调用进行一些奇特的工作,以确保前缀适合MVC解析,但我不会在这里讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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