我不知道我需要在 MVC4 项目中做什么。它在 MVC3 项目中运行良好。
我试图调用控制器方法来获取 Json 结果。我想在客户端输入用户名后显示名字和姓氏。我使用 getJson 从控制器类中获取 Json。它可以在 mvc3 项目中找到,但是在我移到 MVC4 之后,JQuery 方法没有运行。
我的控制器:
public JsonResult PopulateDetails(EditUserModel model)
{
EditUserModel userResultModel = new EditUserModel();
userResultModel.LastName = "John";
userResultModel.FirstName = "Marry";
return Json(userResultModel, JsonRequestBehavior.AllowGet);
}
我的观点:
<script type="text/javascript">
function PopulateData() {
var user = {};
user.UserName = $("#UserName").val();
$.getJSON("PopulateDetails", user, updateFields); // Call the Function from Controller
};
updateFields = function (data) {
$("#FirstName").val(data.FirstName);
$("#LastName").val(data.LastName);
};
<div class="editor-field">
@*@Html.EditorFor(model => model.UserName)*@
@Html.TextBoxFor(model => model.UserName, new { tabindex = "1", onblur = "javascript:PopulateData();" } )
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
请让我知道 MVC4 项目中有什么问题。
谢谢..