如何将模型从视图传递到控制器

3
我有一个以下的视图页面,
      @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>
          } 

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


你想要捕获模型的控制器动作是什么样子的? - Oleksii Aza
3个回答

0

你好,试试这样做:

视图

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

控制器

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

0

使用@Html助手来控制您的控件。

看一下Scott Gu的这篇博客文章。虽然是关于MVC2的,但仍适用于MVC4。

对于更具体的示例,请查看有关@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]装饰的控制器操作,并将您的模型作为参数传递。

0

现有代码存在什么问题?

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

纠正措施

假设在您的情况下不需要TextBox,则可以为那些需要发送到Controller Post Action方法的View Model属性放置隐藏字段

例如

@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,你能分享一下你的模型吗? - SCV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接