I have a weird problem with #Html.ActionLink: it doesn't do anything. It doesn't go to the controller or load the view. I have a project with a menu. From the menu I want to redirect to another view. When I use #Html.ActionLink("Avioane", "Index", "Avioane", null, null) it doesn't go to controller.
public class AvioaneController : Controller
{
// [HttpGet]
public ActionResult Index()
{
//var db = new Curse_AerieneEntities();
//List<Avioane> avs = db.Avioane.Where(z => z.Disponibil).ToList();
//return View(avs);
return View();
}
};
When I use #Ajax.ActionLink("Avioane", "Index", "Avioane", new AjaxOptions{HttpMethod = "GET"}) and set [HTTPGet] on the controller, it goes to the view but doesn't load the view into the page.
What am I doing wrong?
Related
I am using asp.net mvc application.
In my view page i have textbox like below code:
#Html.TextBoxFor(m => m.Id, new { #class = "form-control", #readonly = "readonly" })
Also, in form submit ajax post,
$('#btnSubmit').click(function(e)
{
$.ajax({
url: '/button/Button',
type: 'POST',
data: {Id:1},
});
});
My controller:
MasterM empcode = new MasterM();
[HttpGet]
public ActionResult Button()
{
var empcode = new MasterM();
return View(empcode);
}
[HttpPost]
public ActionResult Button(MasterM model)
{
ModelState.Clear();
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
after this form submit the value of the text box should change to new value (1). How can i achieve this in controller section itself.
I know in ajax success we can manually update the element value but i want to achieve this in controller section and update model with new values.
When you are Posting your are sending only a Single Id and not the Entire Model ,
So basically this
public ActionResult Button(MasterM model) will be empty
and since you are returning back the empty set the text box will not be updated.
There are 2 things you can do here :
1) Modify the Receiving type to
Public ActionResult Button(int Id)
{
MasterM model = new MasterM();
model.Id = Id;
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
2) If you want to retain Model as your Param in Controller
you need to serialize the Data in Jquery and post it .
I have a orchard project, where I have created a module in MVC. I want to pass the id of particular user to controller using #Html.ActionLink but it not calling controller. Here is my code:
In view:
#Html.ActionLink("100111", "AddToCart", "ShoppingCart", new { id = 101 }, null)
//also tried,
#Html.ActionLink("102829", "AddToCart", "ShoppingCart", new { id = 1, area = "OnlineShopping" },null)
In Controller:
[HttpPost]
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
[Themed]
public ActionResult Index()
{
// Create a new shape using the "New" property of IOrchardServices.
var shape = _services.New.ShoppingCart();
// Return a ShapeResult
return new ShapeResult(this, shape);
}
It is not calling the action method because of [HttpPost] and clicking on anchor tag actually does a Get. So try remove the [HttpPost] attribute decoration from your AddToCart action.
[HttpPost]//<--Remove this
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
Found this on ASP.NET MVC ActionLink and post method
You can't use an ActionLink because that just renders an anchor tag.
You can use a JQuery AJAX post, see
http://docs.jquery.com/Ajax/jQuery.post or just call the form's submit
method with or without JQuery (which would be non-AJAX), perhaps in
the onclick event of whatever control takes your fancy.
I am teaching myself asp .net mvc3.
I want to create a user account page which has 3 tabs in it - say YourAddress, YourPhotos and YourProfile.
The 3rd tab (YourProfile) has 2 more subtabs in it ... ChangeDetails and DeactiaveAccount.
They are all dynamic pages and therefore I want to keep them as separate pages.
Basically the urls would be:
localhost/MyHome/YourAddress
localhost/MyHome/YourPhotos
localhost/MyHome/YourProfile/ChangePassword
localhost/MyHome/YourProfile/DeactivateAccount
(As requested I have changed the generic tab1, tab2 etc to something in real-world scenario)
I am planning to do something like this:
public class MyHomeController : Controller
{
//
// GET: /MyHome/Tab1
public ActionResult Tab1()
{
return View();
}
//
// GET: /MyHome/Tab2
public ActionResult Tab2()
{
return View();
}
//
// GET: /MyHome/Tab3
public ActionResult Tab3()
{
return View();
}
}
How do I handle the subtabs of YourProfile? How do I call a controller within a controller?
What is the best way to accomplish this.
Thanks
Have separate action method for each tab item in your controller.
public class MyHomeController : Controller
{
public ActionResult YourAddress()
{
return View();
}
public ActionResult YourPhotos()
{
return View();
}
public ActionResult YouProfile()
{
return VieW();
}
public ActionResult ChangePassword()
{
return View();
}
public ActionResult DeActivate()
{
return View();
}
}
For the sub tab content, define that route in the global.asax
routes.MapRoute("ChangePass","YourProfile/ChangePassword",
new { controller="MyHome", action="ChangePassword" });
routes.MapRoute("DeActivate","YourProfile/DeActivate",
new { controller="MyHome", action="DeActivate" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Always use Url.Action Html Helper method to render a path to an action method.
<div id="tabs">
<ul>
<li>Address</li>
<li>Photos</li>
</ul>
</div>
So I am a little confused as to how to handle some MVC Routing
I have an AdminController
public class AdminController : Controller
{
//
// GET: /Admin/
public ActionResult Index()
{
return View();
}
public ActionResult Users()
{
return View();
}
public ActionResult Books()
{
return View();
}
}
Which works fine. So I can go to /Admin/Books
This is the admin menu for managing books. Now in there I'd like to be able to route like
/Admin/Books/ViewBook/10
or
/Admin/Books/Add
Something like that. I can't seem to grasp how to route these things that way.
I made a controller
AdminBookController
public class AdminBooksController : Controller
{
//
// GET: /AdminBooks/
public ActionResult List()
{
return View();
}
public ActionResult Add()
{
return View();
}
[HttpGet]
public ViewResult BookDetails(Guid guid)
{
return View();
}
[HttpPost]
public ViewResult BookDetails(ModifyBook Book)
{
if (ModelState.IsValid)
return View("Book successfully Edited!");
else
return View();
}
}
}
but I don't want it to be /AdminBooks I feel like /Admin/Books/Action/Param is much nicer.
Thanks in Advance!
If you want those urls to map to your AdminBooks controller, you'll need to map the following routes (in this order):
// maps /Admin/Books/ViewBook/{id} to AdminBooksController.BookDetails(id)
routes.MapRoute(
"AdminBooks_ViewBook", // Route name
"Admin/Books/ViewBook/{id}", // URL with parameters
new { controller = "AdminBooks", action = "BookDetails", id = UrlParameter.Optional } // Parameter defaults
);
// maps /Admin/Books/{action}/{id} to AdminBooksController.{Action}(id)
routes.MapRoute(
"AdminBooks_Default", // Route name
"Admin/Books/{action}/{id}", // URL with parameters
new { controller = "AdminBooks", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
Note: be sure to put these mappings before the default MVC route.
Consider creating an Admin Area and adding a BookController to that Area. See the following link for a walkthrough:
http://msdn.microsoft.com/en-us/library/ee671793.aspx
You can add a new route in your Global.asax file.
See this question:
Use MVC routing to alias a controller
Hey, one more newbie here, just playing around with .NET MVC. My main task is to have a few semi-static pages on URLs like:
/about/
/about/contacts/
/about/jobs/
I'm using a controller for that called Static and have the following route attached:
routes.MapRoute(
"About",
"about/{id}",
new { controller = "Static", action = "Index", id = UrlParameter.Optional }
);
It seems to work fine as I have the Static controller with the Index method which uses a switch statement to identify which page has to be viewed. I use the RedirectToAction() function to call other actions of the Static controller in order to display pages with other views. My views are:
/Static/About.aspx
/Static/Contacts.aspx
/Static/Jobs.aspx
This method seems to work fine, but what I don't like about it is the redirect, so browsing to /about/contacts I get a redirect to /Static/Contacts which is not what I'd really like to see in the URL.
So my question is - what is the correct way of doing this? And is there a way to explicitly call a certain view from my Index action?
Thanks,
~ K.
Don't do the redirect. Instead of using a switch statement within the Index action, have a separate action for each page (ie. About, Contacts, Job) each with their own view.
Your Static controller could look something like this:
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contacts()
{
return View();
}
public ActionResult Jobs()
{
return View();
}
And if you needed to do any processing specific to Contacts or Jobs, it can be done within their respective actions.
To explicitly call a certain view:
return View("ViewName");
There are seven overloads for the View() method. A number of which allow you to pass the Model:
return View("ViewName", Model);
I'd recommend moving away from Static, and have an About controller. Within that controller, one method per page.
public ActionResult About()
{
return View ("About");
}
//Jobs() and Contacts() follow the same pattern
3 routes to match:
routes.MapRoute(
"Jobs",
"about/jobs",
new { controller = "About", action = "Jobs" }
);
routes.MapRoute(
"Contact",
"about/contact",
new { controller = "About", action = "Contact" }
);
routes.MapRoute(
"About",
"about/",
new { controller = "About", action = "About" }
);
you cant return views from a different controller and have the first controller in the URL.
the only way is use your about controller.
so place your logic in your about controller.
i have the same for myself in my Admin controller. it's just a page with some static links, and some extra pages.
in the index.aspx i have
<ul>
<li>
<%= Html.ActionLink("Evaluaties", "Evaluaties", "Admin")%></li>
<li>
<%= Html.ActionLink("ONAS aanbieders", "Index", "ONASAanbieder")%></li>
<li>
<%= Html.ActionLink("ONAS aanbod", "Index", "ONASAanbod")%></li>
<li>
<%= Html.ActionLink("Helpbox", "Index", "HelpBox")%></li>
</ul>
in the controller I have
public ActionResult Index() {
return View();
}
public ActionResult Evaluaties() {
return View();
}
this works like you described, no need to alter the routes.
obviously i have an Evaluaties.aspx in my Admin folder in the Views folder.
gives me this URL: http://localhost:50152/Admin/Evaluaties