-2

I have a button. When I click on it, a controller should be invoked and a function called, which should return either true or false.

How should I indicate it in the UI?

I.e. if the function returns true, I want to write "Success" in the UI. If it returns false then I'd like to write "Failed" in the UI.

How could I do that?

[HttpPost]
public ViewResult DoSomething()
{
    //Call to method
}

@using (Html.BeginForm("DoSomething", "Controller")) {
}
Mark
  • 2,380
  • 11
  • 29
  • 49
Jean Tehhe
  • 1,247
  • 5
  • 19
  • 38
  • If your question is _"How to pass a value from controller to view"_, try reading an MVC tutorial. This can be done using models, ViewBag, ViewData, TempData or Session, depending on your requirements. See also [ViewBag, ViewData and TempData](http://stackoverflow.com/questions/7993263/viewbag-viewdata-and-tempdata). – CodeCaster May 12 '14 at 14:04
  • I guess you need an Ajax request, wich call your controller method and return a boolean value then you can edit the textfield in your callback (in javascript). – Bertrand C. May 12 '14 at 14:08

1 Answers1

1

The most primitive way is to use the ViewBag; so in the action you might do something like this:

ViewBag.Success = callToMethod();

and then in the view for the action, you can get at that like this:

@if (ViewBag.Success != null && (bool)ViewBag.Success)
{
    // render something
}
else
{
    // render something
}

There are of course other ways. It could be that you already have a view model for the view and you just need to add a boolean flag to the view model. If you did something like that, then instead of grabbing it off the ViewBag, you might (and this depends on your @model type) access it like this, Model.Success, instead. And of course, if you added it to the view model, in the action you'd set the result of the method to the instance of the view model.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • @CodeCaster, if you've found that perfect reference question you ought to have voted to close it as a duplicate. You voted to close it because you felt the OP should have done research you *thought* was common sense. Often times we get haughty and high-minded believing we are something we are not and forget where we came from. Often times there are tasks that we want to perform and we **don't even know where to start** or **what to search for.** But alas, we've forgotten that haven't we? – Mike Perrenoud May 12 '14 at 14:11
  • The only thing I forgot is that comments aren't for discussion, nevermind. I close-voted _"Unclear what you're asking"_ and asked for clarification using a comment. If that comment isn't answered, the question can't be properly answered. Duplicate votes don't work, because people always fail to see the commonalities and won't vote accordingly. You may be helping OP, but you aren't helping the site. [Read this to understand my stance in this](http://meta.stackoverflow.com/questions/254341/a-car-with-square-wheels/254362#254362). – CodeCaster May 12 '14 at 14:16
  • 1
    @CodeCaster: I have changed my position and I now stand in your corner; albeit for a different reason I believe. Do keep in mind reputation means nothing to me; I've earned enough it doesn't even matter now. – Mike Perrenoud May 12 '14 at 18:40
  • @CodeCaster: please see my [stance here](http://meta.stackoverflow.com/questions/254358/why-the-backlash-against-poor-questions/254584#254584). – Mike Perrenoud May 12 '14 at 18:45