When user click this element, I want catch id data in controller or in OnActionExecuting method in ActionFilter class.
How I can do this?
In view:
<a id="123" href="AreaName/ControllerName">TEST</a>
You could try this...
In Controller:
public ActionResult Index()
{
var model = new HomeViewModel { Id = 123 };
return View(model);
}
public void RecordClick(int id)
{
int incomingId = id;
}
In View:
#Html.ActionLink("Link Text", "RecordClick", "Home", new { id = #Model.Id }, null)
Generated HTML:
Link Text
Upon clicking link, id value will be sent to RecordClick action.
TEST
your controller
[HttpGet]
public ActionResult Index(int userid)
{
return View();
}
if you debug you should have that value
Related
this is the controller
public ActionResult Test() {
#ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = theId});
}
on the view of Action Named Details I will check if it has the ViewBag to show and show it:
#{
if(ViewBag.TheMessageIs != null){
#ViewBag.TheMessageIs
}
}
but here the redirection is working fine to the page, it's not show the message I have stored in ViewBag.TheMessageIs
thanks
Basically what you're doing is invoking the method Details from your Index method and since you're already overloading your Details action with an id, pass it the message as well:
public ActionResult Index()
{
//ViewBag.TheMessageIs = "this is the message";
return RedirectToAction("Details", new { id = 1, TheMessageIs = "this is the message" });
}
public ActionResult Details(int id, string TheMessageIs)
{
ViewBag.TheMessageIs = TheMessageIs;
return View();
}
Then in the Details view you can access the property like this:
#ViewBag.TheMessageIs
public ActionResult Test() {
TempData["shortMessage"] = "MyMessage";
return RedirectToAction("Details", new { id = theId});
}
public ActionResult Details {
//now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
ViewBag.TheMessageIs = TempData["shortMessage"].ToString();
return View();
}
You have to do it like this since the viewbag looses its value when you redirect to another active / view
So I have a controller and I can seem to understand how to pass a parameter to my ActionResult method.
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{name}/{id}",
defaults: new { controller = "Project", name = "Search", id = UrlParameter.Optional }
);
This is my route. Now in my controller i've created a method
[HttpGet]
public ActionResult Search()
{
return View();
}
[HttpPost]
public ActionResult Search(int Id)
{
ViewBag.iD = Id;
return View();
}
And in my view
<body>
<div>
ASDF + #ViewBag.iD
</div>
</body>
How can I pass a value to my iD parameter from Search Action? It seems whatever I call
http://localhost:52992/Project/Search/id=2
or http://localhost:52992/Project/Search/1
Both method go into the Search() method, none goes to Search(int iD).
What Am I missing?
A link in your view (or a form with FormMethod.Get or entering a url in the address bar) makes a GET call, not a POST, so your method should be
[HttpGet]
public ActionResult Search(int ID)
{
// do something based on the value of ID
ViewBag.iD = ID;
return View();
}
and delete the [HttpPost] method.
You have to pass value from the HttpGet 'SearchAction' method. if you pass from it, then only the value will be shown in the view
[HttpGet]
public ActionResult Search()
{
ViewBag.iD = your Id value here;
return View();
}
on intial load the get method will be called, on submission only the 'post' method will be call.
hope this helps.
On your view
<a href='#Url.Action("ActionName", "ControllerName", new { id= 10})'>...</a>
OR
#{
int id = 10
}
...
On Your Action
Public ActionResult Search(int id)
{
Viewbag.Id = id;
return view();
}
Action is by default on [HTTPGET] you wont have to mention it
My controller name is "demo". I write 2 actions with the same name "Index". The first uses [HttpGet] and the seconds is [HttpPost].
But, when I require a PostBack from View, the value of ViewBag.Name in the action [HttpGet] public ActionResult Index() {} can't be cleared.
[HttpGet]
public ActionResult Index()
{
ViewBag.Name = "HttpGet";
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
ViewBag.Name = "HttpPost";
return View();
}
In RouteConfig.cs:
routes.MapRoute(
name: "newroute",
url: "demo/index/{type}",
defaults: new { controller = "demo", action = "Index", type = UrlParameter.Optional }
);
and the View:
<form method="post" action="#Url.Action("Index", "demo", new { type = #ViewBag.Name })">
<input type="submit" value="Click me" />
</form>
#ViewBag.Name
Here is my problem: When I click the button, the value of #ViewBag.Name in the page is "HttpPost". But, in URL, it's /demo/index/HttpGet
Why?
If you navigate to this page with a GET request, you're executing method Index(), and as the page is rendered the Name is HttpGet, so it will create the URL for the form action as /demo/index/HttpGet.
Later, once you press the button, you're posting to that very URL created in the previous step, but since the form is POSTing you're executing Index(FormCollection form), and that sets Name to HttpPost. The URL remains what it was generated at the previous step.
Try it :
[HttpGet]
public ActionResult Index()
{
ViewBag.Name = "HttpGet";
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form)
{
ViewBag.Name = "HttpPost";
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult AddToCart(int phoneListingID, string sellerSKU)
{
ShoppingBasket shoppingBasket = new ShoppingBasket();
BasketItem currentItem = new BasketItem
{
sellerID = 1,
Price = 100,
Quantity = 1,
sellerSKU = "testsku"
};
shoppingBasket.AddtoBasket(currentItem, this.HttpContext);
var viewModel = new BasketViewModel
{
basketItems = ShoppingBasket.GetBasketItems(this.HttpContext),
basketTotal = ShoppingBasket.GetBasketTotal(this.HttpContext)
};
return View(viewModel);
}
My form:
#using (Html.BeginForm("AddToCart","ShoppingBasket",new { phoneListingID = 12345, sellerSKU = "test"}, FormMethod.Post ))
{
<input type="submit" value="AddToCart" />
}
The expected result is that my BasketViewModel page is returned, however the view being returned is ShoppingBasket/AddToCart?PhoneID=xxxx&sellerSKU=xxxx
What am I doing wrong?
In MVC Suppose your action is like
public ActionResult MyAction()
{
return View();
}
In this scenerio it will point to the view named 'MyAction'. If you want to send it to another view make it like
public ActionResult MyAction()
{
return View("MyViewName");
}
If you want to pass some model to make it like
public ActionResult MyAction()
{
return View("MyViewName",model); // Here model is your object of model class
}
In you snippet your are returning default i.e. 'AddToCart' view because you are not describing explicitly. Make your code like
return View("BasketViewModel",viewModel); // where BasketViewModel is your view name
You're returning that controller's View, if you wish to transfer to another view try
return BasketViewActionResult(viewmodel)
Then access your 'BasketViewActionResult'
Function BasketViewActionResult(model as BasketViewModel) as ActionResult
return View(model)
End Function
Sorry if you don't get VB, I can translate it to C# for you if you wish.
Edit:
You can also simply change the form's action.
#using (Html.BeginForm("BasketView","ShoppingBasket",...
and make all your manipulations within that actionresult
I am really confused,
here is the code :
[HttpPost]
public ActionResult Settings(string SubmitButton)
{
if (SubmitButton == "Sign In") {
ServiceLocator.Current.GetInstance<IAppContext>().LoggedUser = null;
Response.Cookies["loginuser"].Expires = DateTime.Now;
return RedirectToAction("Logon", "Account");
}
if (SubmitButton == "Sign Up") { return RedirectToAction("register", "Account"); }
if (SubmitButton == "Change Default Ride Settings") { return RedirectToAction("changeSettings", "Home"); }
return View();
}
The view contain
<% using (Html.BeginForm()) { %>
Three input ,
<% } %>
the controller is not fired with httppost but fired with httpget
You probably need to pass in the controller and action names in Html.BeginForm() in your view. Since the [HttpPost] Settings() action is being invoked for HTTP get requests, that implies that there isn't another Settings() action for get requests, so I'm guessing that your view is being served from a different action. In such a case, you need to explicitly set the controller and action in your Html.BeginForm(). Try this:
<% using (Html.BeginForm("Settings", "YourControllerName")) { %>
You have to generate a html form with the method attribute set to post if you want a post to happen:
Html.BeginForm("action","controller", FormMethod.Post) { ... }
There should be action with name Index() and should not containg any parameters in it. This is the problem I have faced.
I have used ActionName() to solve the same problem,
Not working code:
[HttpGet]
public ViewResult RsvpForm()
{
[HttpPost]
public ViewResult RsvpFrom()
{
}
Working code:
[HttpGet]
public ViewResult RsvpForm()
{
}
[HttpPost, ActionName("RsvpForm")]
public ViewResult RsvpFromPost()
{
}
The proper way using razor
#using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "form1" }))
{
//form content
}