سؤال

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")) {
}
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top