gpt4 book ai didi

javascript - 提醒 Javascript 作为操作的返回响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:55 28 4
gpt4 key购买 nike

我该怎么做,例如,我在 users.cshtml View 中创建一个用户,它验证 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>

这在我的 Controller 操作中..

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"); ???????

我可以这样做吗?

最佳答案

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

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

然后在 Users.cshtml View (由您重定向到的 Users 操作返回)中测试是否存在此消息并显示 警报:

@if (TempData["message"] != null) {
<script type="text/javascript">
alert(@Html.Raw(Json.Encode(TempData["message"])));
</script>
}

关于javascript - 提醒 Javascript 作为操作的返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479283/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com