MVC 3 navigate to correct ActionResult - asp.net-mvc

In my MVC3 project, I have one controller "MyDetailsController" in the
Areas -> Test -> Controller Folder
And in From the ActionResult "Create" in MyDetailsController, I want to call the ActionResult "Edit" of "DetailsController" Which is located in the Controller folder of my application
This is the code I tried
public ActionResult Create()
{
//Some Code
return RedirectToAction("Edit", "Details", new { id = Party.PartyID });
}
But its not loading the exact ActionResult I need.
The the URL am getting is http://localhost:53970/Test/Details/Edit/977612
The URL I needed is http://localhost:53970/Details/Edit/977612
Any help will be appreciated, Thank you.

This will work :
return RedirectToAction("Edit", "DetailsController", new { id = Party.PartyID ,area = ""});

Related

Open a link in new tab from controller in ASP.NET MVC

I want to display a hyperlink in browser and when I click on the hyperlink the request goes to my controller and open the URL in new tab of the browser.
Any idea how can I achieve this?
#*This is index.cshtml*#
#{
ViewBag.Title = "Index";
}
<h2>Click on the link below</h2>
#Html.ActionLink("GOOGLE", "NewWindow", "Home")
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult NewWindow()
{
return Content("<script>window.open('https://www.google.com/','_blank')</script>");
}
}
}
This code is showing error. Please help
Use target="_blank" to open a link in a new window.
#Html.ActionLink("GOOGLE", "RedirectToGoogle", "Home", null, new { target = "_blank" })
In the controller, return a redirection to the required URL:
public ActionResult RedirectToGoogle()
{
return Redirect("https://www.google.com/");
}

Url.Action map a wrong link from Route attribute

This is target controller and action:
[RoutePrefix("Editor")]
public class EditorController : Controller
[HttpGet]
[Route("{id:int}")]
public ActionResult Edit(int id)
Map method calling:
#Url.Action("Edit", "Editor", new { id = page.Id})
result:
/Editor?id=1
required result:
/Editor/1
To achieve the result you want you have to use a route name:
[HttpGet]
[Route("{id:int}", Name = "EditorById")]
public ActionResult Edit(int id)
Then in your view you would use Url.RouteUrl instead of Url.Action:
#Url.RouteUrl("EditorById", new { controller = "Editor", Id = 1, action = "Edit" })
Hope this helps,
Have you checked if you have enabled MVC AttributeRoutes?
routes.MapMvcAttributeRoutes();
see http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
I just faced with same problem. When i fixed the links - editing is broken (form always redirects to the same page).
Here is solution:
A link
#Html.ActionLink("Edit my nice object", "Edit", new { id=item.Id })
A form in the view Edit.cshtml (specifying Controller name is necessary!)
#using (Html.BeginForm("EditConfirmed", "AppServers"))
The actions in the controller
public class AppServersController
[Route("edit/{id:int?}")]
public ActionResult Edit(int? id)
{
// bla-bla
}
[Route("edit_confirmed")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditConfirmed([Bind(Exclude = "Created,LastModified")] AppServerVM appServer)
{
if (!ModelState.IsValid) return View("Edit", appServer);
// bla-bla
}
}
Now both links and editing works.

Image Handler not working with empty parameter in ASP.NET MVC

I have tried to use default parameters, regex, and nothing works for the URL "/Assets/Images/". I keep getting a 404 error saying it cant be found. It works with just "/Assets/Images/0".
routes.Add(
"Images",
new Route(
"Assets/Images/{*Id}",
new ImageRouteHandler(new ImageHandler())
)
);
You need to add an action to AssetsController called Images
public class AssetsController : Controller
{
public ActionResult Images()
{
return View();
}
[HttpGet]
public ActionResult Images(int id)
{
return View();
}
}

ASP.NET MVC: routing help

Consider two methods on the controller CustomerController.cs:
//URL to be http://mysite/Customer/
public ActionResult Index()
{
return View("ListCustomers");
}
//URL to be http://mysite/Customer/8
public ActionResult View(int id)
{
return View("ViewCustomer");
}
How would you setup your routes to accommodate this requirement?
How would you use Html.ActionLink when creating a link to the View page?
In global.asax.cs, add following (suppose you use the default mvc visual studio template)
Route.MapRoute("Customer",
"Customer/{id}",
new { Controller = "CustomerController", action="View", id="" });
Make sure you put this route before the default route in the template
You then need to modify your controller. For the view,
public ActionResult View(int? id)
{
if (id == null)
{
return RedirectToAction("Index"); // So that it will list all the customer
}
//...The rest follows
}
For your second question, ActionLink is simple.
Html.ActionLink("Link Text", "View", "Customer", new {id=1}, null);

How can I shared controller logic in ASP.NET MVC for 2 controllers, where they are overriden

I am trying to implement user-friendly URLS, while keeping the existing routes, and was able to do so using the ActionName tag on top of my controller (Can you overload controller methods in ASP.NET MVC?)
I have 2 controllers:
ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName) { ... }
public ActionResult Index(long id) { ... }
Basically, what I am trying to do is I store the user-friendly URL in the database for each project.
If the user enters the URL /Project/TopSecretProject/, the action UserFriendlyProjectIndex gets called. I do a database lookup and if everything checks out, I want to apply the exact same logic that is used in the Index action.
I am basically trying to avoid writing duplicate code. I know I can separate the common logic into another method, but I wanted to see if there is a built-in way of doing this in ASP.NET MVC.
Any suggestions?
I tried the following and I go the View could not be found error message:
[ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName)
{
var filteredProjectName = projectName.EscapeString().Trim();
if (string.IsNullOrEmpty(filteredProjectName))
return RedirectToAction("PageNotFound", "Error");
using (var db = new PIMPEntities())
{
var project = db.Project.Where(p => p.UserFriendlyUrl == filteredProjectName).FirstOrDefault();
if (project == null)
return RedirectToAction("PageNotFound", "Error");
return View(Index(project.ProjectId));
}
}
Here's the error message:
The view 'UserFriendlyProjectIndex' or its master could not be found. The following locations were searched:
~/Views/Project/UserFriendlyProjectIndex.aspx
~/Views/Project/UserFriendlyProjectIndex.ascx
~/Views/Shared/UserFriendlyProjectIndex.aspx
~/Views/Shared/UserFriendlyProjectIndex.ascx
Project\UserFriendlyProjectIndex.spark
Shared\UserFriendlyProjectIndex.spark
I am using the SparkViewEngine as the view engine and LINQ-to-Entities, if that helps.
thank you!
Just as an addition this this, it might pay to optimize it to only hit the database once for the project...
ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName)
{
//...
//var project = ...;
return IndexView(project);
}
public ActionResult Index(long id)
{
//...
//var project = ...;
return IndexView(project);
}
private ViewResult IndexView(Project project)
{
//...
return View("Index", project);
}
Sorry, it looks like I am answering my own question!
I returned the call to Index controller inside my "wrapper" controller and then I specified the view name in the Index controller.
ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName)
{
//...
//var project = ...;
return Index(project.ProjectId);
}
public ActionResult Index(long id)
{
//...
return View("Index", project);
}

Resources