【问题标题】:How to Pass Model from view to Controller如何将模型从视图传递到控制器
【发布时间】:2013-07-08 07:33:12
【问题描述】:

我有以下视图页面,

      @using (Html.BeginForm())
              {
              <fieldset class="fs">
           @foreach (var item in Model.lstTravelReadyEntities)
               {   
     <label class="Detail1"><b>Associate Id : </b>@item.Var_AssoId </label>
     <label class="Detail1"><b>Vertical :</b>@item.Var_Vertical</label>
     <label class="Detail1"><b>Visa ValidFrom :</b>@item.Dt_VisaValidFrom </label><br /><br />

     <label class="Detail2"><b>Associate Name :</b>@item.Var_AssociateName</label>
     <label class="Detail2"><b>Account Name :</b>@item.Var_AccountName</label>
     <label class="Detail2"><b>Visa ValidDate :</b>@item.Dt_VisaValidTill</label><br /><br />

     <label class="Detail3"><b>Grade HR :</b>@item.Var_Grade</label>
     <label class="Detail3"><b>Project Name :</b>@item.Var_Project_Desc</label><br />          
               }
                    <h2> Response Details</h2><br />
      Supervisor Response :<input type="radio" class="radi" 
       name="radio" value="yes"   onclick="javascript:Getfunc(this.value);">Yes
       <input type="radio" 
           name="radio" value="no" 
        onclick="javascript:Getfunc(this.value)">No
             <div id="es"></div>
           <input type="submit" id="insert" value="Submit" 
                name="Submit" onclick="javascript:InsertDetails(item);"/>
           </fieldset>
          } 

我想将此视图页面的所有值作为参数传递给控制器​​,以便将这些值插入新表中。我该如何实现?

【问题讨论】:

  • 你的控制器动作在你想要捕捉模型的地方看起来如何?

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

为您的控件使用 @Html 助手。

看看this blog entry from Scott Gu。这是关于 MVC2 但仍然适用于 MVC4。

如需更具体的示例,请查看at this question 关于@Html.RadioButtonFor()

另外,我建议使用 jquery 而非内联 onclick= html 属性来挂钩您的事件。

<script type="text/javascript">
    $("form radio").click(function(){ // or whatever selector you need
        Getfunc($(this)[0].value);
    });
</script>

最后,您需要确保您的 @Html.BeginForm 发布到控制器上的 [HttpPost]-decorated 操作,该操作将您的模型作为参数。

【讨论】:

    【解决方案2】:

    现有代码有什么问题?

    表单中没有输入类型文本控件,这就是信息未发送到服务器的原因。 TextBox 类控件将数据转发给Controller Action Method。

    纠正措施

    假设在您的情况下 TextBox 不是必需的。然后,您可以为那些需要发送到控制器 Post Action 方法的 View Model 属性放置Hidden Fields

    例子

    @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
    {
        <fieldset class="fs">
            @foreach (var item in Model.lstTravelReadyEntities)
            {   
                <label class="Detail1">
                    <b>Associate Id : </b>@item.Var_AssoId
                </label>
                @Html.HiddenFor(i=> item.Var_AssoId) //Added Hidden Field to send this to server
                <label class="Detail1">
                    <b>Vertical :</b>@item.Var_Vertical</label>
                @Html.HiddenFor(i => item.Var_Vertical)  //When post this Hidden Field will send
                <label class="Detail1">
                    <b>Visa ValidFrom :</b>@item.Dt_VisaValidFrom
                </label>
                @Html.HiddenFor(i => item.Dt_VisaValidFrom)
                <br />
            }
            <h2>
                Response Details</h2>
            <br />
        </fieldset>
    }
    

    为了解释观点,我排除了一些控件。现在,您可以为那些需要发送到操作方法的属性添加隐藏字段。

    此外,您应该使用视图模型而不是将单个参数传递给操作方法。

    希望这会对你有所帮助。

    【讨论】:

    • @user2514925 你能分享你的模型吗?
    【解决方案3】:

    你好,试试这样,

    查看

    @using (Html.BeginForm("SaveAudit", "Controller", FormMethod.Post)
    {  
    }
    

    控制器

    [HttpPost]
    public ActionResult SaveAudit(AuditModel model, FormCollection collection)
    {
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多