Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc3 Problem passing data from View to Controller

I am using mvcContrib to generate a grid to allow users to filter data by keying in search data. There are several partial views that are rendered in my Index View:

Here is the partial view that handles the searching:

@model CRMNPS.Models.PagedViewModel<CRMNPS.Models.NPSProcessed>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{

    <label>
Model Number:&nbsp;&nbsp; @Html.TextBox("searchWord" )
<br /><br />From Date:&nbsp;&nbsp;&nbsp; @Html.EditorFor(m => m.FromDate)
</label>
<label>
<Br /><br />To Date:&nbsp;&nbsp;&nbsp; @Html.EditorFor(m => m.ToDate)
</label>    
<label>
<br /><br />&nbsp;&nbsp;<input class="button" value="Search" type="submit" />
<br />
</label>

}

Here is my Index view:

@model PagedViewModel <CRMNPS.Models.NPSProcessed>

@{
    ViewBag.Title = "CRM Processed List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Processed List</h2>
@{Html.RenderPartial("SearchBox");}
@{Html.RenderPartial("Pager", Model.PagedList);}
@Html.Grid(Model.PagedList).AutoGenerateColumns().Columns(column =>{
column.For(x => Html.ActionQueryLink(x.ModelNumber, "Edit", new { id = x.Id
})).Named("Id").InsertAt(1);
}).Sort(Model.GridSortOptions).Attributes(@class => "grid-style")

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { FromDate = Model.FromDate, ToDate = Model.ToDate, SearchWord = Model.SearchWord }))
{
   <p>
       <input class="button" value="Export to Excel" type="submit" />
   </p>
}    

At the bottom of the Index View I have another submit within the Html.BeginForm with a Formmethod.Post.

The Index ActionResult that calls this view passes a viewmodel with the search criteria and a IQueryable object that the mvcContrib uses.

When the user presses the Export to Excel push button I would like to pass the selected values back to the Index actionresult HttpPost controller. (FromDate, ToDate and SearchWord)

The FromDate, ToDate and SearchWord values always come back null.

I am fairly new to MVC so any constructive comments are welcome.

Thanks

Joe

like image 622
Joe Pitz Avatar asked Sep 22 '11 17:09

Joe Pitz


People also ask

How can you transfer data from view to controller?

You can do it with ViewModels like how you passed data from your controller to view. and in your HttpPost action, use a parameter with same name as the textbox name. If you want to post to another controller, you may use this overload of the BeginForm method.

How do you pass data from controller to view and from view to controller?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Can we pass Viewbag from view to controller?

Yes you cannot pass a Viewbag from view to controller. But you can pass them using TempData. Add this to your View. But this TempData passes the information as an object.

Can we use TempData to pass data from controller to view?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller.


1 Answers

Since they are not in the form that you are posting - (Export to Excel is in a separate form). The inputs

FromDate, ToDate and SearchWord

Are in the first form (in the partial view). So those values don't show up in the controller (since they are not part of the http post). If you want to see all these values being passed back to the controller, they should be under one

Html.BeginForm

like image 145
MoXplod Avatar answered Oct 04 '22 23:10

MoXplod