Solved

TextBoxFor value not passed to model

Posted on 2016-07-21
4
65 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
  • 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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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