Is it possible to call jquery pop up from code behind in mvc3.
I am doing couple of validations on page through code behind and wants to show the validation success message.
I tried using ScriptManager.RegisterStartCLient but looks like it is deprecated.
Is there anything like this in mvc3?
Not sure what code behind means in context of ASP.NET MVC. But possibly you should pass your validation message from Controller to View using ViewBag. For example:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Your message";
return View();
}
}
And then in View render the script with razor if message is presence:
#if (ViewBag.Message != null) {
<script>$('#example').some_popup_plugin("#ViewBag.Message")</script>
}
I added a property to the viewModel and when I was rendering the view , I checked the property and showed the jquery pop up.
Something like :
#if(Model.IsSUccess)
showDialog();
Related
I am relatively new to asp.net mvc but I have worked on asp.net webforms. The issue i am facing is I have button on my view page if I click certain action needs to be done and alert message has to be thrown. I could easily achieve this in asp.net webforms with a onclick event and return back to same page but I am unable to do this asp.net mvc
Action method in controller:
Public void loaddata()
{
//Some logic
Viewbag.message="success"
}
In view page:
<scripts>
#if(Viewbag.message!= null)
{
//javascript alert box logic
}
<scripts>
This code works till controller action method but after finishing that it doesn't go to the view where I have the button
Your controllen action is returning void, so nothing goes back to your view.
In MVC this is a bad practice. If you really don't want to return a view or partialView, you can also return a different type of ActionResult.
https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.actionresult?view=aspnet-mvc-5.2
Returning a HttpStatusCodeResult or JsonResult might help you in this situation.
I'm trying to implement a ViewComponent that includes a form with a submit button. Upon clicking the button, the form is submitted and a record is added to the database along with returning a success message. This is all done asynchronously so I simply return a status to the current razor page being viewed.
I want to embed this form/functionality across many pages in my application so that is why im looking at a ViewComponent. I dont want to have to implement the same OnPost functionality for every Razor Page that I add this form to.
If that makes any sense?
Here's how I show the form on a razor page.
#await Component.InvokeAsync("QuoteForm", new Models.Quote())
Here's my simple invoke method.
//Show a ViewComponent with form
public IViewComponentResult Invoke()
{
Quote quote = new Quote();
//Return View with ViewModel from below.
return View("QuoteFormView", quote);
}
Can I do something like this with a ViewComponent?
public async Task<IViewComponentResult> OnPostAsync(Quote quote)
{
//Model did not pass validation.
if (!ModelState.IsValid)
{
//Do Something
}
else
{
//Do database work and send email
//return status to view.
return Content("Database updated and Email Sent.");
}
}
How do I render a full fledged view (not partial view) inside another view?
Scenario, I have different controller and want the exactly same view to render which is already there under other controller with different layout.
I have Wishlist page in Home Controller which shows list of added products, and when user logged in , when I click on wish list it also show me navigation when user is signed in.
How would I do that??
Not many developers know about this but you can use RenderPage, it's specifically designed for that purpose(to render an MVC view inside another view)
#RenderPage("~/Views/Shared/SampleView.cshtml")
You can still create or use a partial view with its own controller and use the RenderAction()
[ChildActionOnly]
public ActionResult ActionPartialView(string p1)
{
//code...
return PartialView();
}
The above code can be in any controller, its own controller, just call it in razor with that controller.
Razor:
#{ Html.RenderAction("Index", "Home"); }
Hope that helps
I have a page that has a search function in a controller which works well
[HttpPost]
public virtual ActionResult Search(SearchModel model)
{
...adds to IEnumerable and such
return View(model);
}
My problem is I have another page with a search box, which I need to redirect to the same view as above. (parameters in the URL is not an option)
#using (Html.BeginForm("Search", "Home", FormMethod.Post, null))
{
#Html.TextBoxFor(t => t.SearchModel)
<input.....
}
but it's not loading up the right URL, it's just adding it to the current one. so instead of example.com/Home/Search it's adding it to the end of where that form is currently located. So if the page was in example.com/About/SearchPage its adding the t.SearchModel to the About/Searchpage
Edit: I have two different Controllers and Views, ControllerA and ViewA works fine, it brings back the search results. I want ViewB, which has an input box, to call ViewA and use ControllerA search technique
You have a nested form. This is not valid HTML. HTML does not allow you to put one form inside another form. You can have more than one form on a page, but you cannot nest them.
I'm also not sure what you mean by "another section", do you mean the #section keyword in Razor? Or do you mean an MVC area? Or something else?
On my site, I want to display a Login box on all pages. So I wanted to make a partial view page, which I use on my _Layout.cshtml file in Shared.
But where would the controller for this partial view go? And how would my Login button have access to it?
So, when the Request.IsAuthenticated is true, the login box shows 'Logged in as ...', but when the result is false, I get a little table with the usual Username/Password form.
Edit: After trying some answers below, I seem to be stuck in an endless lopp on the GET method below. It it because my partial view is trying to load me _Layout.cshtml file, as it want to accosiate the 'masterpage' with the partial view? And because my partial view is being rendered in _Layout.cshtml, it's lopping?
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if(ModelState.IsValid)
{
var g = new GallaryImage();
var user = g.LoginUser(loginModel.Username, loginModel.Password);
if(user != null)
{
FormsAuthentication.SetAuthCookie(user.username, false);
return RedirectToAction("index", "Home");
}
ModelState.AddModelError("", "Invalid Username/Password");
}
return View();
}
In my _Layout.cshtml, I am trying to load the partial view like this:
<div style="text-align: right">
#Html.Action("Index", "Login")
</div>
See the issue?
FYI, Views don't have controllers. Controllers have Views. The distinction may seem subtle, but it's not. A view can be used by any number of controllers, and views don't care or know about the controllers. So you have to think about the current URL, which means the current Action Method.
In the case of your login partial, it doesn't need much if anything from the Controller. It's directly accessing the User property of the page to find out if it's authenticated. Your login button is just a form with it's action method set to your login method of your account controller.
Even the username can be displayed from the User property of the page.
Just look at the default MVC app that is generated when you create a new Internet project. It has all this functionality already implemented. Just copy it.
You can give controller name as attribute to Html.Action method
Html.Action("ActionName", "ControllerName")
If you use Razor syntax, you must write #Html.Action("ActionName", "ControllerName") in your view and your Action with name ActionName should return PartialView(which wil be your login area).
Try to use HTML.RenderAction("Action", "Controller") and make sure you are not calling your master page from within the partial view. This has the potential to produce a loop.
I'm using MVC 3 ASPX view engine not razor though.