Avatar of Scott Baldridge
Scott Baldridge
 asked on

TextBoxFor value not passed to model

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?
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
inxni

8/22/2022 - Mon
inxni

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
Scott Baldridge

ASKER
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

Scott Baldridge

ASKER
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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
inxni

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.