Solved

TextBoxFor value not passed to model

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now