ASP.NET MVC controller not receiving the parameter from view - asp.net-mvc

I am getting null value for the actor(which is an action parameter). I tried all possible ways to call the action method. I don't know if there is any setting in the web.config or what. When I try it in sample MVC application, it worked fine(getting parameters as expected). But the same thing is not working in my current working project. Please help me.
Html:
<table>
<tr>
<th>Time Info</th>
<th>Actor</th>
<th>Reset</th>
</tr>
#{
List<LockedUser> lockedUsers = ViewBag.LockedUsers;
foreach (LockedUser lockedUser in lockedUsers)
{
<tr>
<td>#lockedUser.TimeInfo</td>
<td>#lockedUser.Actor</td>
<td>#Html.ActionLink("Reset", "Reset", "Admin", new { actor = "John" }, null)</td></tr>
}
}
</table>
Action in AdminController:
public ActionResult Reset(string actor)
{
if (System.Web.HttpContext.Current.Cache.Get(actor) != null)
{
System.Web.HttpContext.Current.Cache.Remove(actor);
Debug.WriteLine("Reset Successfull");
ViewBag.Message = "Reset Successfull";
}
else
{
ViewBag.Message = "Unable to reset";
}
return View();
}
RouteConfig:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{WCP}",
defaults: new { controller = "EmailConfirm", action = "Index", WCP = UrlParameter.Optional }
);

Try this:
<td>#Html.ActionLink("Reset", "Reset", "Admin", new { WCP= "John" }, null)</td></tr>

Related

HTTP POST fails when view has default name (route config)

The post of a view fails. It seems like the action name is missed.
If I change the POST action name in both controller and view to another arbitrary name, it works.
Controller
public class AuthenticationController : Controller
{
// GET: Authentication
[HttpGet]
public ActionResult Index()
{
var model = new IndexViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(IndexViewModel model)
{
if (ModelState.IsValid)
{
if(Membership.ValidateUser(model.Username, model.Password))
{
return RedirectToAction("Index", "Default");
}
else
{
model.ErrorMessage = "Failed to login. Please try again!";
}
}
return View(model);
}
}
View
#model Test.Web.Models.Authentication.IndexViewModel
#{
ViewBag.Title = "Log In";
}
<h2>Enter credentials to log on</h2>
#using (Html.BeginForm("Index", "Authentication"))
{
<p>
#Html.LabelFor(x => x.Username)<br />
#Html.TextBoxFor(x => x.Username)
</p>
<p>
#Html.LabelFor(x => x.Password)<br />
#Html.TextBoxFor(x => x.Password)
</p>
<input type="submit" value="Log In" />
}
I'm pretty sure the reason is the RouteConfig and the default action name.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
}
One solution is of course to rename both the GET and POST action, and not use Index, but is there a way of getting this to work as it is? Without any hack, that is.

BeginForm Always calling Index when submitting

I'm using MVC BeginForm to submit form data to my ActionMethod in Controller. The problem is that when every I click on submit button it keeps calling Index method instead of Actual method defined in BeginForm.
Here is my View
#model HRMS.DBModel.department
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm("save", "Department", FormMethod.Post))
{
#Html.TextAreaFor(model => model.Name, new { #class = "form-control" })
<input type="submit" value="submit" />
}
and here is the Department Controller
public class DepartmentController : Controller
{
// GET: Department
public ActionResult Index()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult save()
{
return View();
}
}
and RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Location", action = "Index", id = UrlParameter.Optional }
);
}
I searched on google even found solutions but still my problem is still there.
Any help will be appreciated.
Thanks
Issue Resolved there was a form tag inside my Master page due to which it was calling Index Method, I removed that form tag and now its working fine

MVC ActionLink 404 Error

Hi Guys' I am getting HTTP 404 in my MVC project and I am not sure where it is coming from.
On the _NavBar I have an ActionLink control that is calling to CreateSite
<li>
#Html.ActionLink("Create Location","CreateSite", "SiteRegistration", routeValues: null, htmlAttributes: new { id = "registerLink" })
</li>
I have an Controller name SiteRegistrationController
public class SiteRegistrationController : Controller
{
SiteInfoManager manager = new SiteInfoManager();
//
// GET: /SiteRegistration/
public ActionResult UserSiteResult()
{
return View();
}
}
The views I have are Folder SiteRegistration and CreateSite.cshtml.
The Error is coming from the SiteInfoManager line in the Controller.
Any help would be great.
You need to update your #Html.ActionLink to:
#Html.ActionLink("Create Location","UserSiteResult", "SiteRegistration", new { id = "registerLink" }, null)
And update your Controller:
public ActionResult UserSiteResult(string id)
{
// you can use id now, as it will be regirsterLink
return View();
}

ASP.NET MVC Search Results Page MapRoute Doesn't Work

How can i set mapRoute for search results page? My code doesn't work.
Global.asax.cs
routes.MapRoute(
name: "SearchResults",
url: "{action}/{Keyword}",
defaults: new { controller = "Home", action = "Search" }
);
Search Form
#using (Html.BeginForm("Search", "Home", FormMethod.Get))
{
#Html.TextBox("Keyword",null , new { #class = "SearchBox" })
<input type="submit" value="Search" />
}
HomeController.cs
public ActionResult Search(string Keyword)
{
GamesContext db = new GamesContext();
var SearchResults= (from i in db.Games where i.GameName.Contains(Keyword) || i.GameDesc.Contains(Keyword) select i).Take(20).ToList();
return View(SearchResults.AsEnumerable());
}
This one works for me (should be before default route):
routes.MapRoute(
"SearchResults",
"Search/{Keyword}",
new { controller = "Search", action = "SearchAction" }
);
Creating an ActionLink and MapRoute that there is a constant name in it
And there's a point to use new controller for search instead of home with this route.

MVC Html.BeginForm using Areas

I'm using MVC areas and on a view that's in an area called "Test" I would like to have a form that posts to the following method:
area: Security
controller: AccountController
method: logon
How can I make this happen with Html.BeginForm? Can it be done?
For those of you that want to know how to get it to work with the default mvc4 template
#using (Html.BeginForm("LogOff", "Account", new { area = ""}, FormMethod.Post, new { id = "logoutForm" }))
Try this:
Html.BeginForm("logon", "Account", new {area="Security"})
Try specifying the area, controller, action as RouteValues
#using (Html.BeginForm( new { area = "security", controller = "account", action = "logon" } ))
{
...
}
Use this for area with HTML Attributes
#using (Html.BeginForm(
"Course",
"Assign",
new { area = "School" },
FormMethod.Get,
new { #class = "form_section", id = "form_course" }))
{
...
}
#using (Html.BeginForm("", "", FormMethod.Post, new { id = "logoutForm", action = "/Account/LogOff" }))
{#Html.AntiForgeryToken()
<a class="signout" href="javascript:document.getElementById('logoutForm').submit()">logout</a>
}
For Ajax BeginForm we can use this
Ajax.BeginForm("IndexSearch", "Upload", new { area = "CapacityPlan" }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = updateTarget }, new { id = "search-form", role = "search" })

Resources