Solved

TextBoxFor value not passed to model

Posted on 2016-07-21
4
76 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error import assembly 19 26
asp.net, radiobuttonlist, c# 3 69
C# LINQ query question 11 32
C# Web service insert into Oracle table 8 39
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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