在post之后从控制器返回partial时,JQuery在javascript中不起作用

时间:2016-01-13 18:15:48

标签: jquery asp.net http-post http-get

我在视觉工作室制作一个asp.net项目。我有两个控制器函数[HttpGet] AccountCreate和[HttpPost] AccountCreate。 Get返回带有模型AccountCreatePageModel的PartialView。一切都在这里工作,我的javascript和jquery是可靠的。当我提交表单时,我检查以确保输入是唯一的,如果不是,我发送部分视图的方式与Get相同。但是当它加载并尝试运行javascript时,每个使用$ for jquery的地方都会中断,所以我猜它不知道jquery是什么了。怎么会这样?这一切都与Get。一样完成。

    [HttpGet]
    public PartialViewResult AccountCreate(Guid functionId, long? parentId)
    {
        AccountCreatePageModel model = new AccountCreatePageModel
        {
            Function = DataFactory.SystemAccess.GetFunction(functionId),
            ParentId = parentId
        };

        if (parentId != default(long?))
        {
            model.Account = DataFactory.PartyAccess.GetAccount((long)parentId);
            model.Organization = DataFactory.PartyAccess.GetOrganizationByAccountId(model.Account.Id);
            model.Person = DataFactory.PartyAccess.InitializePerson();
            model.Account.Id = (long)parentId;                
        }
        else
        {
            model.Account = DataFactory.PartyAccess.InitializeAccount();
            model.Organization = DataFactory.PartyAccess.InitializeOrganization();
            model.Person = DataFactory.PartyAccess.InitializePerson();
            model.Address = DataFactory.PartyAccess.InitializeAddress();
        }

        model.PhoneNumber = new PhoneNumberModel();

        return PartialView(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AccountCreate(AccountCreatePageModel model)
    {

        long id = 0;
        long phoneNumberParentId = 0;
        long accountId = 0;

        if (model.Account.Id > 0)
        {
            accountId = model.Account.Id;
        }
        else
        {
            accountId = DataFactory.PartyAccess.UpsertAccount(model.Account, true).Id;
            model.Function = DataFactory.SystemAccess.GetFunction(model.Function.Id);
            if (accountId == -1)     //Account.AccountId already exists, send message back to user
            {
                return PartialView(model); //this returns my partial back
            }
        }

这是razor HTML

@using Application.Web.Extensions
@model Application.Web.Models.AccountCreatePageModel

@if ((Model.Organization.Id == 0) && (Model.Person.Id == 0))
{
    <script type="text/javascript">
        $(function () {
            $('#Organization_Name').rules('remove');
            $('#Person_LastName').rules('remove');
            $('#btnSave').click( function (e) {

                if (($('#Organization_Name').val() == "" &&         $('#Person_LastName').val() === "") || ($('#Person_FirstName').val() !== "" && $('#Person_LastName').val() === "")) {
                    e.preventDefault();
                    document.getElementById("OrgLastNameError").innerHTML =      "Organization Name or Last Name must be provided";
                }
                if ($('#Person_FirstName').val() !== "" &&   $('#Person_LastName').val() === "") {
                    e.preventDefault();
                    document.getElementById("OrgLastNameError").innerHTML =   "Last Name must be provided";
                    $('#Person_LastName').focus();
                }

                //if($('#'))

            });
        });
    </script>
}
@Html.HiddenFor(model => model.Function.Id)
<div class="row">
    <div id="OrgLastNameError" class="text-danger"></div>
</div>

<div class="row">
    @Html.PartialFor(model => Model.Account, "_AccountEdit", new    ViewDataDictionary { { "Readonly", (Model.Account.Id > 0) } })
</div>
<div class="row">@Html.PartialFor(model => Model.Organization,   "_OrganizationEdit", new ViewDataDictionary { { "Readonly",   (Model.Organization.Id > 0) } })</div>

<div class="row">@Html.PartialFor(model => Model.Person, "_PersonEdit")  </div>

1 个答案:

答案 0 :(得分:0)

我没有加入评论讨论的声誉,所以我会在这里发布。这里有很多好的答案:

Javascript Runtime error: $ is undefined

我还想说你没有完全利用这个模型。我建议处理模型本身内控制器中发生的验证。在您的控制器中,您可以添加:

        if (accountId == -1)    
        {
            ModelState.AddModelError("", "Your account id already exists.")
            return PartialView(model); //this returns my partial back
        }

然后您可以在视图中显示:

Html.ValidationSummary(true)

此外,我发现您在本地验证字段,由于多种原因,这可能会出现问题。 MVC还为我们提供了在模型中添加验证的选项。如果您添加到模型属性:

[Required]
public string MyModelProperty{get; set;}

当用户尝试在该字段中没有任何数据的情况下提交表单时,模型将抛出错误。