JQuery getJSON在MVC4项目中不起作用

时间:2013-03-14 04:22:27

标签: asp.net-mvc-4 getjson

我不知道我需要在MVC4项目中做些什么。它在MVC3项目中运行良好。

我试图调用控制器方法来获取Json Result。我想在客户端输入UserName后显示名字和姓氏。我使用getJson从控制器类中获取Json。它正在mvc3项目中找到工作,但在我转移到MVC4之后,JQuery Method没有运行。

我的控制器:

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项目有什么问题。

谢谢..

1 个答案:

答案 0 :(得分:0)

我将所有脚本放入

@section Scripts { 

           <script type="text/JavaScript"> {  // My Function  } }

然后为我工作。