3

我该怎么做,例如,我在 users.cshtml 视图中创建一个用户,它验证 ActionResult Create(RegisterModel um) 如果一切正常,我想在 users.cshtml 返回,但总是使用一个 javascript 警报或类似的, 带有来自动作的变量的值。我可以做这个吗?

我有这个在我看来..

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Crear Usuario</legend>

     <div class="editor-label">
        Username:
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div>

    <div class="editor-label">
        Password:
    </div>
    <div class="editor-field">
        @Html.PasswordFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>

            <div class="editor-label">
        Repite Password:
    </div>
    <div class="editor-field">
        @Html.PasswordFor(model => model.ConfirmPassword)
        @Html.ValidationMessageFor(model => model.ConfirmPassword)
    </div>
    </div>
    <p>
        <input type="submit" value="Crear" />
    </p>
</fieldset>

这在我的控制器动作中..

public ActionResult Create(RegisterModel um)
{

    if (um.Password == um.ConfirmPassword)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus;
        Membership.CreateUser(um.UserName, um.Password, um.Email, um.PasswordAnswer, um.PasswordQuestion, true, null, out createStatus);

        if (createStatus == MembershipCreateStatus.Success)
        {
            var alert = MembershipCreateStatus.Success.ToString();
        }
        else
        {
            ModelState.AddModelError("", ErrorCodeToString(createStatus));

            var alert = ErrorCodeToString(createStatus);
        }
    }
        //HERE IS WHERE I WANT TO RETURN TO /ADMIN/USERS BUT WITH AN ALERT WITH CONTAINING THE VALUE OF alert IN A JAVASCRIPT OR SIMILAR ALERT WINDOW

    return RedirectToAction("Users", "Admin"); ???????

我可以这样做吗?

4

2 回答 2

8

您可以TempData在重定向到操作之前将消息存储在其中Users

TempData["message"] = "some message that you want to display";
return RedirectToAction("Users", "Admin");

然后在Users.cshtml视图内部(由Users您重定向到的操作返回)测试此消息的存在并显示alert

@if (TempData["message"] != null) {
    <script type="text/javascript">
        alert(@Html.Raw(Json.Encode(TempData["message"])));
    </script>
}
于 2012-05-07T09:48:44.837 回答
0

我试图在同一个视图中显示错误,而不是尝试达林的答案,我尝试了其他类似的使用ViewBag通过返回在另一个页面中显示错误Partial View

最后我尝试使用TempData, 在发布期间维护消息,这里我使用引导程序显示错误

动作方法:

[HttpPost]
public ActionResult RegDetail(User_table usob, FormCollection col)
{
 // some other code here
if (!sqlReader.HasRows)
{
TempData["message"] = "Contains some message";
return RedirectToAction("Registration");
}
return RedirectToAction("Index");
}

我的注册视图:

@if (TempData["message"] != null)
{
<link href="~/css/boostrap.css" rel="stylesheet" />
<script src="~/js/boostrap.min.js"></script>

<div class="alert alert-danger">

<a class="close" data-dismiss="alert">&times;</a>

<strong style="width:12px">Error!</strong> Thats wrong code no, try entering correct!

</div>

}

如果您需要使用 Tempdata 进行更多探索,您可以查看Mr.James 使用引导消息解释的地方,希望对某人有所帮助

于 2015-04-13T16:02:10.743 回答