3

how can i do, for example, i create a user in users.cshtml view, that it validates to ActionResult Create(RegisterModel um) and if its all ok, i want to return at users.cshtml but always with one javascript alert or similar, with the value of a variable from the action. Can i do this one?

I have it this in my view..

@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 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 it something like this?

bombai
  • 927
  • 1
  • 17
  • 27

2 Answers2

8

You could store the message inside TempData just before redirecting to the Users action:

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

and then inside the Users.cshtml view (which is returned by the Users action to which you redirected) test for the presence of this message and display an alert:

@if (TempData["message"] != null) {
    <script type="text/javascript">
        alert(@Html.Raw(Json.Encode(TempData["message"])));
    </script>
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thank you darin, instead of trying yours, i wasted some hours, i have posted answer below using bootstrap – Shaiju T Apr 13 '15 at 16:04
0

i was trying to show error in same view, instead of trying darin's answer i tried other like this using ViewBag and this to show error in another page by returning Partial View

finally i tried using TempData, to maintain the message during post, here i am showing error using bootstrap

action method:

[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");
}

my registration view:

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

}

if you need to explore more using Tempdata you can check this where Mr.James explains using bootstrap messages, hope helps someone

Community
  • 1
  • 1
Shaiju T
  • 6,201
  • 20
  • 104
  • 196