【问题标题】:Data does not refresh in browser but does in database数据不会在浏览器中刷新,但会在数据库中刷新
【发布时间】:2014-10-21 16:35:48
【问题描述】:

我有一个有效的提交按钮,所以当我对表单进行更改并单击提交表单更新时...

但是。在数据库中,它显示新数据。但是当我 RedirectToAction 到最后一页时,它不会显示更新的数据。

这是我的代码(控制器):

[HttpPost]
public ActionResult Save(M2Portal.Areas.Admin.Models.Users.Roles roleForm)
{
    try
    {
        if (ModelState.IsValid)
        {
            var role = Srvctx.Roles.FirstOrDefault(w => w.RoleID == roleForm.RoleId);
            role.RoleDescription = roleForm.RoleDescription;
            Srvctx.SubmitChanges();
            return RedirectToAction("RoleManagement");
        }
        return RedirectToAction("RoleManagement");
    }
    catch
    {
        return RedirectToAction("RoleManagement");
    }
}

所以当它命中时:

                     return RedirectToAction("RoleManagement");

它只是转到页面但不刷新数据。但是当我查看我的数据库时,它已经在那里更改了。

我是 ASP.NET MVC 的新手,不知道从哪里开始寻找......

有什么想法吗?

角色管理:

  public ActionResult RoleManagement(string userName)
    {

        return View("RoleManagement", new UserForm(userName));
    }

角色管理cshtml:

   @foreach (M2DAL.M2Service.Role r in Model.AllRoleIDs)
{
    <tr>
        <td>@r.RoleName</td> //This is not refreshing, on debug shows the number, but browser shows old number)
        <td>@r.RoleID</td>
        <td>@Html.ActionLink("Edit Roles", "EditRole", "Users", new { roleId = r.RoleID }, new { @class = "action" }) </td>
    </tr>
}

【问题讨论】:

  • 发布 RoleManagement 方法的代码 - 问题出在哪里(当模型无效而不是返回视图时,您真的想重定向到 RoleManagement 吗?)
  • iv 添加了角色管理,这只是一个cshtml页面
  • RoleManagement 方法需要一个参数 userName 但您没有向该方法传递任何内容。不确定new UserForm(userName) 做了什么,但userName 将是null。当您重定向时,它应该是return RedirectToAction("RoleManagement", new { userName = "SomeValue" });(并且大概userName 是您在发布表单时在Roles 中的一个属性的值)
  • 当我调试时,用户名确实显示为空,但在 cshtml 中,角色名显示为新数据。但由于某种原因它不在浏览器中!
  • 很难说。我什至不知道new UserForm(null) 做了什么。看看你之前的问题,AllRoleIDsRole 类型的属性,而RoleManagement 返回UserForm 类型的模型,那么这两种类型都有AllRoleIDs 属性吗?我真不知道你在这里做什么。 (ps。将方法放入模型中以访问数据库不是很好的做法)

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

您没有发布RoleManagement 操作的代码。我不知道您将模型存储在哪里,但您确定此操作正在再次读取数据库吗?

应该有类似的东西(只是一个例子)

[HttpGet]
public ActionResult RoleManagement()
{
    var roles = Srvctx.Roles.Take(1000);
    return roles;
}

【讨论】:

  • .Take(1000) 是什么意思,我也不明白为什么它不刷新是因为 RoleManagement 方法或保存按钮?
  • 正如@stephen-muecke 所说,new UserForm(userName) 背后究竟是什么?看起来您没有在 RoleManagement 操作中从 DB 加载任何数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 2019-06-23
  • 2015-03-16
相关资源
最近更新 更多