Javascript warning as action return response

how can I do this, for example, I create a user in the users.cshtml view, which he checks for ActionResult Create (RegisterModel um), and if all this is normal, I want to return to users.cshtml, but always with one javascript warning or the like , with the value of the variable from the action. Can i do this?

I have it in my opinion.

@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>

And this is in my controller action.

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

Can I do something like this?

+3
source share
2 answers

You can save the message inside TempDataimmediately before redirecting to an action 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>
}
+7

, , , , this, ViewBag , , Partial View

, TempData, , , bootstrap

:

[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, , - , , -

0

All Articles