【发布时间】:2019-04-25 01:25:16
【问题描述】:
我的 ajax 请求有问题,我找不到接收控制器模型的方法。 我搜索了互联网,但没有找到可以解决我问题的答案。
我要做的是在我的表单中显示“结果”中包含的数据(姓名、名字和礼貌)。 然后我希望用户修改这些数据并点击“提交”按钮发送我的 POST 请求。
我的方法看起来是正确的(至少是 GET),我很确定我的问题来自我使用 ajax 的方式。
您能告诉我要编辑的代码吗?
提前谢谢你!
控制器
[HttpGet]
public async Task<IActionResult> GetMember(int id)
{
try
{
FileMakerRestClient client = new FileMakerRestClient("https://fms171.hostmy.solutions", "helloJAK", userName, password);
var toFind = new Models.Members { Zkp_WEB = id };
var results = await client.FindAsync(toFind);
Console.WriteLine(results);
bool isEmpty = !results.Any();
if (isEmpty)
{
return NotFound();
}
Console.WriteLine(results);
return View(results);
}
catch
{
return BadRequest();
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> GetMember(Models.Members model)
{
if (ModelState.IsValid)
{
FileMakerRestClient client = new FileMakerRestClient("https://fms171.hostmy.solutions", "helloJAK", userName, password);
var toCreate = new Models.Members { NameFirst = model.NameFirst, NameLast = model.NameLast, Politeness = model.Politeness };
var results = await client.CreateAsync(toCreate);
return Ok(results.Response);
}
else return BadRequest();
}
查看
@model jak.formulaire.Models.Members
<div id="myForm">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Politeness, "Politeness", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.Politeness, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter politeness", @id = "Politeness" } })
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
@Html.ValidationMessageFor(model => model.Politeness, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Zkp_WEB, "Id", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.Zkp_WEB, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter id", @id = "idMember" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.NameFirst, "First name", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.NameFirst, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter first name", @id = "NameFirst" } })
</div>
<div class="form-group">
@Html.LabelFor(model => model.NameLast, "Last name", htmlAttributes: new { @class = "control-label col-md-4" })
@Html.EditorFor(model => model.NameLast, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter last name", @id = "NameLast" } })
</div>
<br /><br />
<button type="submit" class="btn btn-primary" id="btnEdit">Submit</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
GetMember();
Edit();
});
function GetMember() {
//$('#btnEdit').click(function () {
// var idMember = $('#idMember').val();
$.ajax({
type: "GET",
url: "https://localhost:44338/Members/GetMember/" + 2,
dataType: "json"
});
//});
}
function Edit() {
$('#btnEdit').on('click', function () {
var idMember = $('#idMember').val();
myFormdata = {
Politeness: Politeness,
NameFirst: NameFirst,
NameLast: NameLast
};
$.ajax({
type: "POST",
contentType: "application/json",
url: "https://localhost:44338/Members/GetMember/",
data: JSON.stringify(myFormdata),
dataType: "json" ,
success: function (data) {
if (data === "success") {
alert("User successfully modified");
}
},
error: function (error) {
alert('error');
}
});
});
}
</script>
}
【问题讨论】:
标签: c# jquery asp.net-mvc asp.net-ajax