I can't Hide Url Parameter, - asp.net-mvc-4

#Html.ActionLink("LotNumberDetails", "Index", "LotNumber", new { id = item.lotNUmber }, null)
This is my action link in table, when I click on LotNumber ActionLink it generates the following URL:
http://servername.com/LotNumber/Index/1111_100868781211
The method in controller is as follows:
[HttpGet]
public ActionResult Index(string id)
{
var TupleResult = objLotNumberModel.GetLotNumberValuesEnumerable(id);
return View("Index", TupleResult);
}
Everything is working fine, but when I change ActionLink to Ajax.ActionLink...
#Ajax.ActionLink("LotNumber", "Index", "LotNumber", new { id = item.lotNUmber }, new AjaxOptions { HttpMethod = "POST"})
and the controller method to...
[HttpPost]
public ActionResult Index(string id)
{
var TupleResult = objLotNumberModel.GetLotNumberValuesEnumerable(id);
return View("Index", TupleResult);
}
The method is firing but I can't move to respective page.
My idea is to hide the ID of LotNumber, ie when clicking on ActionLink I Just want to get the URL like "http://servername.com/LotNumber/Index".

Related

Redirecting to a response view with a model does not keep model properties

I have a form view that submits form data to the post action on a controler and then redirects to another view that uses logic to display either a success or failure, but the new view just shows blank values for model properties. Here is the post action:
[HttpPost]
public ActionResult ContactUs(TTT.Models.ContactUsModel model)
{
logger.Info(model.URL + "Contact Us Form submitted");
var userkey = model.ValidationKey;
var sessionkey = Session["ContactUsKey"];
var lastsubmission = Session["ContactUsTime"];
model.Response = "success";
//first check if honeypot was populated via a bot and if so send it to the success page without doing anything
if (model.WorkAddress != "")
{
logger.Info("honeypot triggered");
return View("ContactUsResponse", model);
}
I'll leave out the remainder of the controler, but
And here is the view it's redirecting to:
#using TTT.Models
#using Sitecore.Mvc
#model ContactUsModel
<h1>#Model.Title</h1>
<div>#Model.Body</div>
<div>
#if (#Model.Response == "fail")
{
#Model.Failure;
} else
{
#Model.Success;
}
</div>
Instead of returning a new view, call RedirectToAction and return new view from that controller.
[HttpPost]
public ActionResult ContactUs(TTT.Models.ContactUsModel model)
{
//--- Code omitted for brevity
if (model.WorkAddress != "")
{
logger.Info("honeypot triggered");
return RedirectToAction("ContactUsResponse", new { response = model });
}
}
public ActionResult ContactUsResponse(TTT.Models.ContactUsModel response)
{
return View(model)
}

mvc url.action returning null or url as get

I am using mvc Url.Action as this
<a href="#Url.Action("Index", "Product", new { area = "Product", search = UrlParameter.Optional, categoryId = category.IdCategory })">
I have my routing as:-
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}/{categoryId}",
defaults: new { controller = "Product", action = "Index", search = UrlParameter.Optional, categoryId = #"\d+" },
namespaces: new[] { "IBuyFrontEnd.Areas.Product.Controllers" }
);
}
I cannot get the url to map to this route. I am getting it as
However if i change the action to
#Url.Action("Index", "Product")
I get this as Url
http://localhost/iteric/?action=Index&controller=Product
I cannot figure the why so of this behavior. Just started using .net mvc. Please help me with this.
Ok So figured out that to get Url.Action to generate the url requires the that action to be present in the controller, and also the parameters should be in place. So i just changed my controller action to
public ActionResult Index( int? categoryId,string search)
{
return View();
}

MVC Form action method gets overwritten

I have the following razor form:
#model
#using (Html.BeginForm("ResetPassword", "User", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
#Html.Hidden("guid", ViewData["guid"]) ....ect it contains a model and 1 hidden field
When i hit the page i must pass a guid i do this the following way:
User/ResetPassword/8C5F38CC-C8DB-46B4-80F5-169699D8A583
I hit the action controller as expected:
public ActionResult ResetPassword(string id)
{
ViewBag.Title = #DDHelper.GetContent("user_password_reset_new") + " " +
#DDHelper.GetContent("slogan") + " " + #DDHelper.GetMeta("sitename");
if (id != null)
{
Guid pwID = new Guid();
if (Guid.TryParse(id, out pwID))
{
if (UserManager.GetResetPasswordUser(pwID) != null)
{
ViewData["guid"] = id;
return View(new Models.User());
}
}
}
return View();
}
Now when i look at the html razor produced i see:
<form action="/User/ResetPassword/8C5F38CC-C8DB-46B4-80F5-169699D8A583" class="form-horizontal" method="post" role="form">
When i post the form i want to hit the action:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ResetPassword(Models.User pwUser)
{
string guid = Request["guid"];
string password = pwUser.Password;
Guid pwID = new Guid();
if (Guid.TryParse(guid, out pwID))
{
UserManager.ResetUserPassword(password,pwID);
return RedirectToAction("LogOn");
}
return View(guid);
}
Now when I post the form I hit the cshtml again and i am not hitting my action because the action
/User/ResetPassword/8C5F38CC-C8DB-46B4-80F5-169699D8A583
does not exsist and everytime someone hits this page the guid is different.
How can i tell the html.beginform to not write parameters in the action name? and why is razor behaving like this?

MVC 4 creating slug type url

i am trying to create a stackoverflow like url.
I the following example works fine. But if i remove the controller then it errors out.
http://localhost:12719/Thread/Thread/500/slug-url-text
Note the first Thread is the controller the second is the action.
How can i make the above URL look like the following excluding the controller name from the url?
http://localhost:12719/Thread/500/slug-url-text
My Routes
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new
{
controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""
}); // Parameter defaults )
}
}
Thread Controller
public class ThreadController : Controller
{
//
// GET: /Thread/
public ActionResult Index()
{
string s = URLFriendly("slug-url-text");
string url = "Thread/" + 500 + "/" + s;
return RedirectPermanent(url);
}
public ActionResult Thread(int id, string slug)
{
return View("Index");
}
}
Placing the following route before the default route definition will directly call the 'Thread' action in 'Thread' controller with the 'id' and 'slug' parameter.
routes.MapRoute(
name: "Thread",
url: "Thread/{id}/{slug}",
defaults: new { controller = "Thread", action = "Thread", slug = UrlParameter.Optional },
constraints: new { id = #"\d+" }
);
Then if you really want it to be like stackoverflow, and assume someone enters the id part and not the slug part,
public ActionResult Thread(int id, string slug)
{
if(string.IsNullOrEmpty(slug)){
slug = //Get the slug value from db with the given id
return RedirectToRoute("Thread", new {id = id, slug = slug});
}
return View();
}
hope this helps.

MvcSiteMapProvider get wrong Id parameter

I using MvcSiteMapProvider to create treeview navigator in Asp.net MVC 4
I have 2 link like:
~/Home/Article/{id} and
~/Home/Gallery/{id}
my Treeview like: Home -> Article -> Gallery
And I used dynamic code on Controller
[MvcSiteMapNode(Title = "Article", ParentKey = "Home", Key="Article", PreservedRouteParameters="id")]
public ActionResult Article(int id)
{
ViewBag.Id = id;
return View();
}
[MvcSiteMapNode(Title = "Gallery", Key="Gallery" ParentKey = "Article", PreservedRouteParameters="id")]
public ActionResult Gallery(int id)
{
ViewBag.id = id;
return View();
}
So it run success, but Problem is when i have
~/Home/Article/123 and I go to ~/Home/Gallery/456
Next I click on treeview to go back Article, it set wrong ID parameter in article, It get Gallery's id set for Article's Id look like: ~/Home/Article/456.
Anyone have solver?. Sorry about my english, it bad.
You could explicitly set the name of the parameter.
Eg.
[MvcSiteMapNode(Title = "Article", ParentKey = "Home", Key="Article", PreservedRouteParameters="ArticleId")]
public ActionResult Article(int ArticleId)
{
ViewBag.Id = ArticleId;
return View();
}
[MvcSiteMapNode(Title = "Gallery", Key="Gallery" ParentKey = "Article", PreservedRouteParameters="GalleryId")]
public ActionResult Gallery(int GalleryId)
{
ViewBag.id = GalleryId;
return View();
}
Then:
/Home/Article/123
/Home/Gallery?GalleryId=456&ArticleId=123