Submit Data from partial view to a controller MVC

2020-03-01 00:04发布

问题:

I have a list of employment records, you can also add an employment record from the same page using a partial view.

Heres employment.cshtml that has a partial view for the records list and a partial view to add a new record which appears in a modal pop up.

<h2>Employment Records</h2>

  @{Html.RenderPartial("_employmentlist", Model);}                              

<p>
<a href="#regModal" class="btn btn_b" rel="fancyReg">Add New Record</a>
</p>

<div style="display:none">
    <div id="regModal"> 
              @{Html.RenderPartial("_AddEmployment", new ViewModelEmploymentRecord());}     

                        </div>
                    </div>

Heres the partial view _AddEmployment.cshtml

@using (Html.BeginForm("AddEmployment, Application"))
{                   
@Html.ValidationSummary(true)

<div class="formEl_a">

<fieldset>
    <legend></legend>

     <div class="sepH_b">

            <div class="editor-label">
                @Html.LabelFor(model => model.employerName)
            </div>

         etc....etc....

</fieldset>

</div>
  <p>
        <input type="submit" class="btn btn_d" value="Add New Record" />

    </p>


}

and heres my Application controller:

[HttpPost]
    public ActionResult AddEmployment(ViewModelEmploymentRecord model)
    {
        try
        {
            if (ModelState.IsValid)
            {

                Add Data.....
            }
        }
        catch
        {

        }

        return View(model);
    }

When compiling the following html is generated for the form:

<form action="/Application/Employment?Length=26" method="post"> 

It brings in a length string? and is invoking the Employment controller instead?

Hope all is clear....

QUESTION ONE: when I click the submit button from within the partial view it does not go to the controller specified to add the data. Can anyone see where im going wrong?

QUESTION TWO: When I get this working I would like to update the employment list with the new record....am I going about this the correct way? Any tips appreciated.

回答1:

Answer 1: First try this and let me know if that hits your controller.

 @using (Html.BeginForm("AddEmployment", "Application", FormMethod.Post))

Answer 2: To update the employment list, I would assume you would want to save the model to your database then have your employment list displayed on the same page or a different page calling the data from the DB into the the list or table to be displayed.

Edit: It looks as though your form attributes are not being applied. For your employment.cshtml, I personally don't use { } around my @Html statements. You must not be doing what I stated above because your error occurs only when I write it as

 @using (Html.BeginForm("AddEmployment, Application", FormMethod.Post))

missing those closing quotes is what is causing your problem.



回答2:

jQuery code:

window.jQuery(document).ready(function () {
    $('#btnsave').click(function () {

        var frm = $("form");
        var data = new FormData($("form")[0]);
        debugger;
        $.ajax({
            url: '/Home/Update',
            type: "POST",
            processData: false,
            data: data,
            dataType: 'json',
            contentType: false,
            success: function (response) {
                alert(response);
            },
            error: function (er) { }
        });
        return false;
    });

});

Controller Code

[HttpPost]
    public JsonResult Update(Generation obj)
    {
        if (ModelState.IsValid)
        {
            return Json("done");
        }
        else
        {
            return Json("error create");
        }
    }

Using those code you can post form using jquery and get response in jsonresult



回答3:

you have put @using (Html.BeginForm("AddEmployment, Application")) what this is trying to do is invoke a action called "AddEmployment, Application" i think you meant @using (Html.BeginForm("AddEmployment", "Application"))



回答4:

I know this is very old Question the reason it didn't work for you because your syntax Here is your code

@using (Html.BeginForm("AddEmployment, Application"))

the fix

@using (Html.BeginForm("AddEmployment", "Application"))

Regards