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>
          } 

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

4

3 回答 3

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]控制器上的 -decorated 操作,该操作将您的模型作为参数。

于 2013-07-10T13:31:48.553 回答
0

现有代码中的问题是什么?

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

纠正措施

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

例子

@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>
}

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

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

希望这会帮助你。

于 2013-07-11T12:52:22.030 回答
0

嗨试试这样

看法

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

控制器

[HttpPost]
public ActionResult SaveAudit(AuditModel model, FormCollection collection)
{
}
于 2013-07-10T11:34:30.943 回答