使用编辑表单加载部分视图无法正确重载

发布于 2024-12-21 09:47:48 字数 1574 浏览 1 评论 0原文

我正在尝试使用 jQuery 将 PartialView 编辑表单加载到 div 中。我正在超载 EditUser 操作。第一个用于传递 id 并使用现有详细信息加载表单。第二个用于发回表单以进行保存。但当我使用 jQuery 和 Url.Action 加载时,它似乎调用了第二种方法。如果我注释掉第二个 EditUser 方法,那么它会调用第一个方法。这是为什么?当我传递staffID时,如何让它调用第一个?或者是否有更好的方法在部分视图场景中实现此编辑表单?

CreateUser 操作工作得很好,因为重载方法没有歧义,因为 1 没有参数,另一个有模型作为参数。

谢谢

这是我的控制器:

    public PartialViewResult EditUser(String staffId)
    {
        User um = userService.GetUserDetails(1, staffId, true);
        return PartialView(um);
    }

    [HttpPost]
    public PartialViewResult EditUser(User um)
    {
        if (!TryUpdateModel(um))
        {
            ViewBag.updateError = "Edit Failure";
            return PartialView("EditUser", um);
        }

        userService.CreateUpdateUser(um);
        return PartialView("ViewUser", um);
    }

    public PartialViewResult CreateUser()
    {
        ViewBag.Message = "Create New User";
        return PartialView(new User());
    }

    [HttpPost]
    public ActionResult CreateUser(User um)
    {

        if (!TryUpdateModel(um))
        {
            ViewBag.updateError = "Create Failure";
            return PartialView(um);
        }

        userService.CreateUpdateUser(um);
        return View("Index");
    }

这就是我加载 EditUser 部分视图的方式:

function menuEdit() {
    $('#ActionMenu').hide();
    $('#SearchBar').hide();
    $('#SearchPanel').hide();
    $('#SearchResult').hide();
    $('#AddViewEditUser').load("@Url.Action("EditUser","User")", {staffId : sId});
    $('#AddViewEditUser').show();
}

I am trying to load an edit form which is a PartialView, into a div using jQuery. I am overloading the EditUser action. The 1st one is for passing the id and loading the form with existing details. The 2nd one is for posting back the form for save. But it seems to call the 2nd method when I load using jQuery and Url.Action. If I comment out the 2nd EditUser method, then it calls the 1st method. Why is that? How can I make it call the 1st one when I pass staffID? Or is there a better way to implement this Edit form in partial view scenario??

And the CreateUser action works just fine as there is no ambiguity on the overloaded methods as 1 has no parameters and the other has a model as parameter.

Thanks

This is my controller:

    public PartialViewResult EditUser(String staffId)
    {
        User um = userService.GetUserDetails(1, staffId, true);
        return PartialView(um);
    }

    [HttpPost]
    public PartialViewResult EditUser(User um)
    {
        if (!TryUpdateModel(um))
        {
            ViewBag.updateError = "Edit Failure";
            return PartialView("EditUser", um);
        }

        userService.CreateUpdateUser(um);
        return PartialView("ViewUser", um);
    }

    public PartialViewResult CreateUser()
    {
        ViewBag.Message = "Create New User";
        return PartialView(new User());
    }

    [HttpPost]
    public ActionResult CreateUser(User um)
    {

        if (!TryUpdateModel(um))
        {
            ViewBag.updateError = "Create Failure";
            return PartialView(um);
        }

        userService.CreateUpdateUser(um);
        return View("Index");
    }

This is how I am loading my EditUser partialview:

function menuEdit() {
    $('#ActionMenu').hide();
    $('#SearchBar').hide();
    $('#SearchPanel').hide();
    $('#SearchResult').hide();
    $('#AddViewEditUser').load("@Url.Action("EditUser","User")", {staffId : sId});
    $('#AddViewEditUser').show();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-12-28 09:47:48

根据 jQuery .load() “如果数据作为对象提供,则使用 POST 方法;否则,假设是 GET。”由于您提供数据,.load() 使用方法“POST”,因此您的第二个 EditUser() 被调用。

According to jQuery .load() "The POST method is used if data is provided as an object; otherwise, GET is assumed." Since you are providing data, .load() is using the method "POST", thus your second EditUser() is being called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文