0

I´m newbie in MVC3. I have a button [Generate Password] on my 'Edit' view. I need to execute a function GeneratePsw() defined in 'Admin' controller which returns a string before displaying a modal that will contain the value returned by GeneratePsw().

I also tried to put the value in a ViewBag.pwd instead return it and read it from the Modal. No success

In other words:

The user do click in [Generate Password] button. Then GeneratePsw() is called and returns a string. A Bootstrap modal should appear automatically displaying that value in a label.

In My View.....

<a href="#1" role="button" class="btn btn-primary btn-small" data-toggle="modal" onclick="location.href='@Url.Action("GeneratePsw", "Admin")';return false;"><i class="icon-lock icon-white"></i> Generate Password</a>


</div>


<div id="1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
   <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
         <h3 id="myModalLabel">Password generated</h3>
 </div>
 <div class="modal-body">
      <p><strong>Password: @(ViewBag.pwd)</strong></p>
  </div>
  <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">OK</button>

  </div>
</div>


</td>

My GeneratePsw() function:

[HttpPost]
public ActionResult GeneratePsw()
{

    HomeBridgeEntities ddbb = new HomeBridgeEntities();
    SqlConnection Cn = new SqlConnection(((System.Data.EntityClient.EntityConnection)ddbb.Connection).StoreConnection.ConnectionString);
    SupPassGenerator sup = new SupPassGenerator(Cn);

    string psw = sup.CreateRandomPassword(9);

    ViewBag.psw = psw;

    return RedirectToAction("Edit");

}

1 Answer 1

1

So my understanding is that you want to do this as an ajax call? i.e. not reload the whole page? I also assume you are using jQuery?

You could do it with a postback to your controller that returns JSON. That might be the easiest way to get it back:

Controller:

public ActionResult GeneratePsw()
{
    ...
    string psw = sup.CreateRandomPassword(9);
    var json = new { password = psw };
    return Json(json);
}

js on your page:

$('#yourgeneratebutton').on('click', function() {
  $.getJSON('YourController/GeneratePsw', function(data) {
      $('#yourpasswordp').text(data.password);
      $('#1').modal('show');
   });

Note, I'm using getJSON, so you couldn't decorate the action with HttpPost. (You aren't posting any data anyway? You'd either have to change the http attribute, or use $.post instead. This is also totally untested, just a rough guide.

OR, an alternative might be to return a partial view that has the modal stuff in it and then show that.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thank you so much!! That is exactly what i needed!! Now is working fine thanks to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.