在Net .Core中提交Ajax表单

问题描述 投票:1回答:1

我现在用简单的形式用jQuery-Steps lib创建了向导。没有“提交”按钮。我通过jQuery在完成步骤提交表单。

我已经在各处使用jQuery调用,并且我已经包含了所有脚本,我需要用于上传图像和其他内容的表单,它更容易使用它。

没有任何反应,控制器动作不会被调用。

我刚刚在起始页面上重定向,查询字符串中包含所有这些参数。

像这样:

https://localhost:44380/Dashboard?Name=Aaaa&Surename=Bbbb&City=Cccc

仪表板全部使用Ajax和部分视图。从菜单中的person选项我将在Dashboard中的主视图上重定向所有这些参数。

这是形式:

<form asp-controller="PersonSettings" asp-action="SaveForm" data-ajax="true" data-ajax-method="POST" id="personForm" class="steps-validation wizard-notification">
    <!-- Step 1 -->
    <h6>Person</h6>
    <fieldset>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="Name">
                        Person Name :
                        <span class="danger">*</span>
                    </label>
                    <input autocomplete="off" type="text" class="form-control required" id="Name" name="Name">
                </div>
            </div>
            <div class="col-md-12">
                <div class="form-group">
                    <label for="Surname">
                        Person Surname:
                        <span class="danger">*</span>
                    </label>
                    <input autocomplete="off" type="url" class="form-control required" id="Surname" name="Surname">
                </div>
            </div>
            <div class="col-md-12">
                <div class="form-group">
                    <label for="City">
                        Person City:
                        <span class="danger">*</span>
                    </label>
                    <input autocomplete="off" type="url" class="form-control required" id="City" name="City">
                </div>
            </div>
        </div>
    </fieldset>
</form>

这是模型:

public class PersonDto {
    public string Name { get; set; }
    public string Surname { get; set; }
    public string City { get; set; }
}

这是PersonSettings控制器中的Action:

[Route("SaveForm")]
[HttpPost]
public IActionResult SaveForm(PersonDto Person) {
   //Do something with person model
   return Ok();
}

在完成按钮上,我在这个表单上调用了jQuery:

 $('#personForm').submit();

编辑:

我试过这个参数:

<form action="PersonSettings/SaveForm" method="post" id="personFrom" class="steps-validation wizard-notification">

这有效..它很好,但为什么第一种方法不起作用?因为我需要在Ajax中完成并完成方法。

我没有找到任何方式使用经典Ajax调用的表单发布图像。因为我无法读取图像路径(它没有暴露给浏览器)。

c# asp.net-mvc asp.net-core
1个回答
1
投票

我认为你没有安装Tag Helpers。如果它与action属性一起使用,它应该与Tag Helpers属性一起使用。因为您使用的是Asp-Net-Controller属性。

https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.TagHelpers

请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.