简体   繁体   English

ASP.NET MVC控制器方法中的HTTpPost方法的多个参数类型

[英]Multiple Argument Types for HTTpPost Method in ASP.NET MVC Controller Method

I have a page on which I want to show information in two sets : 我有一个页面,要在其中显示两组信息:

<div id="DivMainContainer">
<fieldset>
    <legend>Settings</legend>
    @using (Ajax.BeginForm("Index", "Settings", null))
    {
        <table id="TableMain" style="width: 100%">
            <tr>
                <td style="width: 50%;vertical-align:top">
                @*@Html.Partial("_TableColumnNames")*@
                  @Html.CheckBoxListFor(model=>model.PostedColumns.ColumnIds,
        model=>model.AllColumns,
        columnItem=>columnItem.Id,
        columnItem=>columnItem.ColumnName,
        model=>model.SelectedColumns,
        Position.Vertical)
                </td>
                <td>
                    <table id="TableGridSettings">
                        <tr>
                            <td class="FirstCell">
                                #Records per page
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                                <label id="LabelGridRowCount" for="InputGridRowCount">
                                    Snapshot Grids</label>
                            </td>
                            <td>
                                <input type="text" id="InputGridRowCountSG" name="InputGridRowCount" />
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                                Data Sets Grid
                            </td>
                            <td>
                                <input type="text" id="InputGridRowCountDST" name="InputGridRowCountDST" />
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                                Data Sources Grid
                            </td>
                            <td>
                                <input type="text" id="InputGridRowCountDSR" name="InputGridRowCountDSR" />
                            </td>
                        </tr>
                        <tr>
                            <td class="FirstCell">
                            </td>
                            <td>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

        <button>
            Save</button>
    }
</fieldset>    

The first cell in this table contains a strongly typed (model-bound) CheckBoxList and the second cell contains three simple text fields to capture information. 该表中的第一个单元格包含一个强类型(绑定到模型)的CheckBoxList,第二个单元格包含三个简单的文本字段来捕获信息。

There is a save button which will cause a post to this method below : 有一个保存按钮,它将在下面导致此方法的发布:

[HttpPost]
    public ActionResult Index(PostedColumns postedColumns) 
   {
        try
        {
            return View(GetColumnsModel(postedColumns));
        }
        catch (Exception ex)
        {


        }
        return View();
    }

The Post method accepts argument of type "PostedColumns" since that is the type of the information posted by the CheckBoxList helper I am using. Post方法接受类型为“ PostedColumns”的参数,因为这是我正在使用的CheckBoxList帮助程序所发布的信息的类型。

Now I also want to collect information from the other three simple controls and that can be done using FormCollection if the post method was revised as below : 现在,我还想从其他三个简单控件中收集信息,并且如果post方法进行了如下修改,则可以使用FormCollection来完成:

[HttpPost]
    public ActionResult Index(FormCollection form)
   {
        try
        {
           //do something like form["txtInputId"]......to read values
        }
        catch (Exception ex)
        {


        }
        return View();
    }

But this means I am not able to now get the posted info from checkboxlist. 但这意味着我现在无法从复选框列表中获取发布的信息。

Looking for some pointers on how to capture/post info from both these controls when the save is clicked. 寻找有关单击保存时如何从这两个控件捕获/发布信息的指示。

Include the other simple fields into the same model itself so that you will be able to get the value from the same model itself on post back. 将其他简单字段包括在同一模型本身中,这样您就可以在回发时从同一模型本身获取值。 Or else create a new wrapper class as a model which will hold the PostedColumns and the properties related to the other controls. 否则,创建一个新的包装器类作为模型,该类将包含PostColumns和与其他控件相关的属性。 Then bind this model to the View you are using. 然后将此模型绑定到您正在使用的视图。 Then change the post action method with the new model that is created and can get the value from the view model. 然后,使用创建的新模型更改后操作方法,该新模型可以从视图模型中获取值。 If you provide the whole view and model it will be easy to check for the solution in much better way. 如果您提供整个视图和模型,则可以轻松地以更好的方式检查解决方案。

As Venkat indicated, creating a view model is your best option but you can also add additional parameters to your POST method 正如Venkat所言,创建视图模型是您的最佳选择,但您也可以在POST方法中添加其他参数

[HttpPost]
public ActionResult Index(PostedColumns postedColumns, string InputGridRowCount, string InputGridRowCountDST, string InputGridRowCountDSR)
{
  ...

or your could manually get the values from Request.Form 或者您可以从Request.Form手动获取值

public ActionResult Index(PostedColumns postedColumns)
{
  string inputGridRowCount = Request.Form["InputGridRowCount"];
  ....

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

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