6

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

1
  • You dont see <br> and &nbsp;&nbsp;&nbsp; quite enough anymore... Commented Sep 22, 2011 at 19:00

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, But the data is in the Model, as Model.FromDate, Model.ToDate and Model.SearchWord. What would be the correct solution?
It looks like you are using the same model everywhere. Try removing begin form from the partial view entirely and encapsulate your entire index view from the <h2> tag in the using beginform statement. This is one of the ways you can do this.
I added the @using (Html.BeginForm("Index", "Home", FormMethod.Post to the SearchBox partial view and the values are still coming back null.
But you still have 2 FORM's on the page. And you are clicking submit on the 2nd one and your input variables are in the 1st one. So the 2nd form is posting back to the controller with nothing in it but a submit button.
Oh, that now makes sense, I guess I am trying to use the same view as both get and post. I did see a post about responding to different submits based on the value or name. I can try this, unless there is a better way? Thx
|
1

One way is to put'em all in the same form as MoXplod suggested or you can use some javascript to send search values as query string by hooking the submit event of second form like

$('#excel-form').live('click', function(){
   var action = this.action;
   var searchString = $('#SearchWord').val();
   var toDateString = $('#ToDate').val();
   var fromDateString = $('#FromDate').val();
   if(action.indexOf('?')<0)
   {
      action = action+"?SearchWord="+searchString;
   }
   else
  {
     action = action+"&SearchWord="+searchString;
  }
  action = action + "&ToDate="+toDateString + "&FromDate=" + fromDateString;
  $(this).attr('action', action);
  return true;

});

it will put these values in querystring and make them available in action method. Alternatively, you can use ajax to post these values to controller rather than full regular post back.

1 Comment

Thanks @Muhammad, if I cannot get the post back to work I will try the ajax method you recommend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.