简体   繁体   English

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

[英]How to Pass Model from view to Controller

I'm having following view page, 我有以下视图页面,

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

I want pass all the values of this view page to the controller as parameters for inserting these values into the new table.How can i Achieve this? 我想将此视图页面的所有值传递给控制器​​作为将这些值插入新表的参数。如何实现这一点?

Hi try like this, 嗨试试这样,

View 视图

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

Controller 调节器

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

Use @Html helpers for your controls. 使用@Html助手为您的控件。

Have a look at this blog entry from Scott Gu . 看看Scott Gu的这篇博客文章 It's about MVC2 but still applies to MVC4. 这是关于MVC2但仍然适用于MVC4。

For a more concrete example, have a look at this question regarding @Html.RadioButtonFor() . 有关更具体的示例,请查看有关@Html.RadioButtonFor() 此问题

Also, I would recommend hooking your events using jquery instead of inline onclick= html attributes. 另外,我建议使用jquery而不是内联onclick= html属性来挂钩事件。

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

Finaly, you will need to make sure your @Html.BeginForm posts to an [HttpPost] -decorated action on your controller that takes your Model as parameter. 最后,您需要确保您的@Html.BeginForm发布到控制器上的[HttpPost]详细操作,该操作将您的模型作为参数。

What is the Problem in Existing code ? 现有代码中的问题是什么?

There is no Input Type Text Control in the form and that's the reason information is not being sent to server. 表单中没有输入类型文本控件,这就是没有将信息发送到服务器的原因。 TextBox like controls forwards the data for sending the information to Controller Action Method. 像控件一样的TextBox转发数据以将信息发送到Controller Action Method。

Corrective Action 纠正措施

Let's say TextBox is not Required in you case. 假设TextBox在你的情况下不是必需的。 Then, you can place Hidden Fields for those View Model Properties which are required to be sent to Controller Post Action method. 然后,您可以为那些需要发送到Controller Post Action方法的View Model Properties放置Hidden Fields

Example

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

For explaining point of view, I excluded some of the controls. 为了解释观点,我排除了一些控件。 Now, You can add Hidden Fields for those Properties which are required to be sent to Action Method. 现在,您可以为那些需要发送到Action Method的属性添加隐藏字段。

Also you should use View Models instead of Passing Individual parameter to Action Method. 您还应该使用“视图模型”而不是“将个别参数传递给操作方法”。

Hope this will help you. 希望这会帮助你。

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

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