asp.net-mvc 将数据从局部视图提交到控制器 MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10300955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:53:39  来源:igfitidea点击:

Submit Data from partial view to a controller MVC

asp.net-mvcrazorasp.net-mvc-partialview

提问by Dez79

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.

这里是具有记录列表部分视图和添加新记录的部分视图的employment.cshtml,该记录出现在模态弹出窗口中。

<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

下面是部分视图 _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:

编译时为表单生成以下html:

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

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

它带来了一个长度字符串?而是调用 Employment 控制器?

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.

问题二:当我开始工作时,我想用新记录更新就业清单......我这样做是否正确?任何提示表示赞赏。

回答by John Sykor

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

回答 1:首先尝试这个,如果它击中您的控制器,请告诉我。

 @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.

答案 2:要更新就业列表,我假设您希望将模型保存到数据库中,然后将您的就业列表显示在同一页面或不同页面上,将数据库中的数据调用到列表或表中显示。

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

编辑:看起来好像您的表单属性没有被应用。对于你的employment.cshtml,我个人不会在@Html 语句周围使用{}。你一定不能做我上面说的,因为你的错误只有当我把它写成

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

missing those closing quotes is what is causing your problem.

缺少那些结束引号是导致您出现问题的原因。

回答by Rakesh Jogani

jQuery code:

jQuery代码:

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

使用这些代码,您可以使用 jquery 发布表单并在 jsonresult 中获得响应

回答by khaled Dehia

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

问候

回答by Manatherin

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"))

你已经把这@using (Html.BeginForm("AddEmployment, Application"))试图做的是调用一个叫做"AddEmployment, Application"我想你的意思的动作@using (Html.BeginForm("AddEmployment", "Application"))