简体   繁体   English

MVC无法使用ajax.beginform从部分视图提交和保存数据

[英]MVC unable to submit and save data from partial view using ajax.beginform

I am new to MVC, but I have been making some changes to an existing code that was created by another group. 我是MVC的新手,但是我一直在对另一个小组创建的现有代码进行一些更改。

There is a User Data view with several partial views, and I want to update the "Profile" section (name, phone, etc). 有一个带有几个局部视图的用户数据视图,我想更新“个人资料”部分(姓名,电话等)。 The thing is that clicking on the "save" button does nothing; 问题是单击“保存”按钮没有任何作用。 setting a breakpoint on the controller doesn't even show the site reaching that code. 在控制器上设置断点甚至不会显示站点已到达该代码。

These are the main portions of the current code. 这些是当前代码的主要部分。

"User Detail" view: “用户详细信息”视图:

@model UserDetailsModel

@{
    Layout = "~/Views/Shared/_ApplicationLayout.cshtml";
}

...

<h2>User Details</h2>
...
<hr />

<div class="row">
    <div class="span2">
    <ul class="nav nav-pills nav-stacked" id="js-side-nav" data-spy="affix">
        <li class="active"><a href="#profile">Profile</a></li>
        <li><a href="#userdevices">Functions</a></li>
...
    </ul>
</div>
<div class="span10">
    <ul class="unstyled">
        <li id="profile">
            <h3>Profile</h3>
            @Html.Partial("_UserProfile", Model.ProfileModel)
            <hr />
        </li>
        <li id="functions">
            <h3>Functions</h3>
            @Html.Partial("_UserFunctions", Model.FunctionsModel)
            <hr />
        </li>
...

"User Profile" partial view: “用户个人资料”部分视图:

// (I added HttpMethod = "Post") //(我添加了HttpMethod =“ Post”)

@model UserProfileModel

<section id="user-profile-section">
    @using (Ajax.BeginForm("UserProfile", "UserDetail", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "user-profile-section", InsertionMode = InsertionMode.Replace}))
{
    @Html.EditorForModel("CustomForm")
    <input class="btn btn-primary" type="submit" value="Save Profile"/>
}
</section>

"User Detail" controller: “用户详细信息”控制器:

//(I added [System.Web.Http.HttpPost]) //(我添加了[System.Web.Http.HttpPost])

[System.Web.Http.HttpPost]
public ActionResult UserProfile(Guid userId, [FromBody] UserProfileModel model)
{
if(!Request.IsAjaxRequest())
    throw new Exception("Expecting ajax request");

if(!ModelState.IsValid)
        return PartialView("_UserProfile", model);

var service = new UserService();
    WebUser user = service.GetUserProfile(userId, SessionState.ApplicationId);
    Mapper.DynamicMap(model, user.Profile, model.GetType(), user.Profile.GetType());
    service.UpdateUser(user);

return PartialView("_UserProfile", model);
}

"User Profile" model: “用户个人资料”模型:

using System.ComponentModel.DataAnnotations;

namespace UserDetail
{
public class UserProfileModel
{
    [Required]
    [Display(Name = "First Name")]
    public string ContactFirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string ContactLastName { get; set; }

    [Display(Name = "Business Phone")]
    public string ContactBusinessPhone { get; set; }

...

ClientValidationEnabled and UnobtrusiveJavaScriptEnabled are set to TRUE on the web.config. 在web.config上将ClientValidationEnabled和UnobtrusiveJavaScriptEnabled设置为TRUE。

I would first like to get the code reach the UserProfile section in the controller, so that I can then make sure the service.UpdateUser() method works. 我首先要使代码到达控制器中的UserProfile部分,以便可以确保service.UpdateUser()方法有效。 I have checked, searched and tried several options and I haven't found what I am missing. 我已经检查,搜索并尝试了几种方法,但没有找到我所缺少的东西。 In the end, obviously, I want to be able to update the data. 最后,显然,我希望能够更新数据。

Thanks. 谢谢。

I would bet you have some field failing validation, but it does not have a corresponding validation message or is hidden, and thus you are not seeing the error. 我敢打赌,您的某些字段验证失败,但是它没有相应的验证消息或被隐藏,因此您看不到错误。 Perhaps to test this just remove the entire contents of the form except for the submit button and see if it successfully submits. 也许要进行测试,只需删除表单中除“提交”按钮之外的所有内容,然后查看它是否成功提交。

Another thing to try just as a troublehshooting step, is change(copy it to a comment to be restored after testing) this 另一个作为故障排除步骤尝试的方法是更改​​(将其复制到注释以在测试后还原)

@using (Ajax.BeginForm("UserProfile", "UserDetail", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "user-profile-section", InsertionMode = InsertionMode.Replace}))

...to a simple Html.BeginForm, just to determine/rule out the possibility that something is wrong with the AJAX here, perhaps not able to find the UpdateTargetId. ...到一个简单的Html.BeginForm,只是为了确定/排除此处AJAX有问题的可能性,也许是找不到UpdateTargetId。

Also, open F12 on Chrome, switch to console tab, refresh page and submit, to see if any javascript errors appear. 另外,在Chrome上打开F12,切换到控制台标签,刷新页面并提交,以查看是否出现任何JavaScript错误。

Lastly, try removing the [FromBody] attribute temporarily as a test. 最后,尝试暂时删除[FromBody]属性作为测试。 Some attributes effect routing, and I'm not sure if this one will, as I've never used it. 一些属性会影响路由,但我不确定此属性是否会成功,因为我从未使用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM