我想将部分 View 返回为html,因此我可以在div中将其渲染为html,但是它不起作用,我无法找到为什么其不起作用的问题,这是我的代码。
function getPartial(id) {
$.ajax({
type: "POST",
url: "/Home/GetPartial",
contentType: "application/html",
data: { ID: id },
success: function (response) {
$(".ui-layout-east").html(response);
alert(response);
}
});
}
在我的 Controller 中,我正在这样做。
[HttpPost]
public ActionResult GetPartial(int ID)
{
var gopal = DataAccess.DataAccess.FillDetailByID(ID);
return PartialView("parent", gopal);
}
但是当我以json格式返回时,则无法正常工作,请帮助我解决该问题。
以下是我要退回的我的部分资料。
@model WebTreeDemo.Models.Employee
<div id="widget">
<div id="x">
@using (Html.BeginForm("Home", "Update", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EmpCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmpCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmpCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobDesc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobDesc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobDesc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfJoining, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfJoining, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfJoining, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="submitButton" value="Save" class="btn btn-default" />
@Html.HiddenFor(x => x.ID)
</div>
</div>
</div>
}
</div> <!-- end of #foo -->
最佳答案
试试这个...
function getPartial(id) {
$.ajax({
type: "POST",
url: "/Home/GetPartial",
contentType: "application/html",
dataType: "html",
data: { ID: id },
success: function (response) {
$(".ui-layout-east").html(response);
alert(response);
}
});
}
关于c# - 如何使用ajax将部分 View 作为html返回,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29204546/