简体   繁体   中英

Transfer Model Data in View to the Controller

I have a model that I am using in my view that is full of data. This data is then edited in the view. I need to figure out a way to resubmit this data back over to the controller.

Here is what I have so far.

VIEW:

@using (Html.BeginForm("DownloadCSV", "Respondents", FormMethod.Post))
{
    @Html.HiddenFor(m => m.FilterSet)

    <div class="btn btn-default pull-right" id="dispoCSV" onclick="$('#csvFormSubmit').click()">
        <i class="icon-file-alt"></i> Disposition Report
    </div>
    <input id="csvFormSubmit" type="submit" style="display:none;" />
}

CONTROLLER:

[HttpPost]
        public ActionResult DownloadCSV(RespondentsFilterSet model)
        {
            string csv = "Charlie, Test";
            return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "DispositionReport.csv");
        }

MODEL:

public class RespondentsFilterSet : ColdListFilterSet
    {
        public List<int> OwningRecruiters { get; set; }

        public List<int> RecruitingGroups { get; set; }

        public override bool HasAtLeastOneFilter()
        {
            return base.HasAtLeastOneFilter() || OwningRecruiters.IsNotNullOrEmpty() || RecruitingGroups.IsNotNullOrEmpty();
        }

        public override ExpressionBase ToExpression()
        {
            var expr = base.ToExpression();

            var expressions = expr == null ? new List<ExpressionBase>() : new List<ExpressionBase> { expr };

            if (OwningRecruiters.IsNotNullOrEmpty())
            {
                expressions.Add(new InExpression<int> { Field = Create.Name<Respondent>(r => r.RecruitedBy), Values = OwningRecruiters });
            }

            if (RecruitingGroups.IsNotNullOrEmpty())
            {
                expressions.Add(new InExpression<int> { Field = Create.Name<Respondent>(r => r.RecruitingGroupId), Values = RecruitingGroups });
            }

            return expressions.Count == 0 ? null : BuildAndExpressionFromList(expressions);
        }
    }

I realize that my controller is not not finalized. I just have displaying some static csv. But I can't figure out why my model from my view is always null when returned to the controller.

Just look at your form. There's not a single input element (except the submit button). You cannot expect to get anything back on the server in this case.

Please read about HTML and how forms work in HTML. In HTML forms you have input fields. Things like text fields, hidden fields, checkboxes, radio buttons, ... - fields that the user interacts with get submitted to the server.

The fact that you have made your HttpPost controller action take some model as parameter doesn't mean at all that this parameter will be initialized. In ASP.NET MVC you have a default model binder. This model binder looks at what gets sent to the server as values when the form is submitted and uses the names of the fields to bind to the corresponding properties. Without input fields in the form, nothing gets sent to the server. Just use the debugging tools built into your web browser to inspect what exactly gets sent to the server.

Contrary to classic ASP.NET WebForms, ASP.NET MVC is stateless. There's no ViewState to remember your model.

So all this rambling is to say that you should read more about HTML forms first and understand the stateless nature of the web before getting into ASP.NET MVC. As far as your particular problem is concerned, well, assuming the user is not supposed to modify any values of the view model in your view throughout some input fields, you could simply include a hidden field containing the id of your model in the form. This id will then be sent to your POST controller action as parameter and you could use it to retrieve your original model from wherever it is stored (I guess a database or something).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM