3

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?

SCV
  • 251
  • 1
  • 12
user2514925
  • 931
  • 8
  • 33
  • 56

3 Answers3

0

Hi try like this,

View

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

Controller

[HttpPost]
public ActionResult SaveAudit(AuditModel model, FormCollection collection)
{
}
Adriano Silva
  • 2,518
  • 1
  • 17
  • 29
Jaimin
  • 7,964
  • 2
  • 25
  • 32
0

Use @Html helpers for your controls.

Have a look at this blog entry from Scott Gu. It's about MVC2 but still applies to MVC4.

For a more concrete example, have a look at this question regarding @Html.RadioButtonFor().

Also, I would recommend hooking your events using jquery instead of inline onclick= html attributes.

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

Community
  • 1
  • 1
Matthew Perron
  • 6,201
  • 2
  • 21
  • 24
0

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.

Corrective Action

Let's say TextBox is not Required in you case. Then, you can place Hidden Fields for those View Model Properties which are required to be sent to Controller Post Action method.

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.

Also you should use View Models instead of Passing Individual parameter to Action Method.

Hope this will help you.

SCV
  • 251
  • 1
  • 12