Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TextBoxFor value not passed to model

Posted on 2016-07-21
4
Medium Priority
?
86 Views
Last Modified: 2016-07-25
Hello, I have the following in my view:

       @Html.TextBoxFor(m => m.LastName, new { @class = "filterInput" })

The user supplies a value and clicks:

@Html.ActionLink("Export to Excel", "ExportToExcel")

Which gets data from the model and then exports the data to excel:

 public void ExportToExcel(PortalAccountMappingModel model)
        {
            var data = model.GetExport();
            ExportHelper.ExportExcel();
        }


In the  model.GetExport(); I pass the value to filter data and return what I need to export.

But the model is not being updated to reflect what the user entered in the TextBoxFor so my data does not get filtered.

Why is that? Is it because I'm not doing an ActionResult and the view never updates the model when the user enters data into the TextBoxFor?
0
Comment
Question by:Scott Baldridge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:inxni
ID: 41724039
Looks like you want to post data to a form but Html.ActionLink only generates a link, it doesn't post values to form. Change it to button and mark your method with HttpPost.

In case you really want to use ActionLink to post, try see here
0
 

Author Comment

by:Scott Baldridge
ID: 41724714
Thanks for responding inxni!
I'm new to MVC and having a hard time figuring out how to deal with this situation. I have a jqgrid in the view which is populated when the view is loaded. There is a search button that allows the user to filter results for the grid. I need to add an additional button to export the grid to excel.
After trying several changes to the [HttpPost] for the view I can't get it to initially load the grid, allow searching/filtering and also allow export.

How can I change the  [HttpPost] to allow for all these functions?

//View
@using (Html.BeginForm())
{
<div class="divImportButton" title="@Resources.Admin_PortalAccountsMapping_ImportLabel">
    <input type="button" value="@Resources.Admin_PortalAccountsMapping_ImportLabel" class="btnImport" />
    <input type="button" value="Export to Excel" />
 </div>

<div class="criteriaContainer">
    
        @Html.LabelFor(m => m.AccountCode, Resources.Admin_PortalAccountsMapping_AccountCode)
        @Html.TextBoxFor(m => m.AccountCode, new { @class = "filterInput" })

        @Html.LabelFor(m => m.AccountName, Resources.Admin_PortalAccountsMapping_AccountName)
        @Html.TextBoxFor(m => m.AccountName, new { @class = "filterInput" })

        @Html.LabelFor(m => m.CustomerName, Resources.Admin_PortalAccountsMapping_CustomerName)
        @Html.TextBoxFor(m => m.CustomerName, new { @class = "filterInput" })

        @Html.LabelFor(m => m.FirstName, Resources.Admin_PortalAccountsMapping_FirstName)
        @Html.TextBoxFor(m => m.FirstName, new { @class = "filterInput" })

        @Html.LabelFor(m => m.LastName, Resources.Admin_PortalAccountsMapping_LastName)
        @Html.TextBoxFor(m => m.LastName, new { @class = "filterInput" })

        @Html.LabelFor(m => m.OnlyAccountWithoutMapping, Resources.Admin_PortalAccountsMapping_OnlyAccountsWithoutMapping)
        @Html.SimpleCheckBoxFor(m => m.OnlyAccountWithoutMapping)

        <input type="submit" value="@Resources.Common_Search" name="button" /> 
</div>
}

@Html.Partial("~/Views/Partial/JqGridView.cshtml", Model.Grid)


//Controller
 [HttpGet]
        [CaseDBAuthorization(PermissionType.PortalAccountsMapping)]
        public ActionResult PortalAccountMapping()
        {
            return View(new PortalAccountMappingModel());
        }

        [HttpPost]
        [CaseDBAuthorization(PermissionType.PortalAccountsMapping)]
        public ActionResult PortalAccountMapping(PortalAccountMappingModel model)
        {
            if (IsJqGrid)
            {
                UpdateModel(model.Grid.UpdateParams);
                return Json(model.GetRows());
            }
            else return null;
        }


//Export code
            var data = model.GetExport();
            ExportHelper.ExportExcel(data, Constants.PORTAL_ACCOUNT_MAPPING_REPORT_NAME, typeof(PortalAccountMappingReportItem));

Open in new window

0
 

Author Comment

by:Scott Baldridge
ID: 41724876
I have changed my [HttpPost] and think I'm almost there! Everything seems to work except when doing a search the jqgrid does not load and the only thing showing in the view is the json data.
What am I doing wrong?
public ActionResult PortalAccountMapping(string button, PortalAccountMappingModel model)
        {
            if (IsJqGrid)
            {
                UpdateModel(model.Grid.UpdateParams);
                return Json(model.GetRows());
            }
            else if (button == "Export to Excel")
            {
                var data = model.GetExport();
                ExportHelper.ExportExcel(data, Constants.PORTAL_ACCOUNT_MAPPING_REPORT_NAME, typeof(PortalAccountMappingReportItem));
                return null;
            }
            else if (button == "Search")
            {
                UpdateModel(model.Grid.UpdateParams);
                return Json(model.GetRows());
            }
            else return null;
        }

Open in new window

0
 
LVL 3

Accepted Solution

by:
inxni earned 2000 total points
ID: 41728408
Instead of return Json, try return a View and pass json data as model.
Like this
return View("ViewName", jsondata);

Open in new window

0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question