#Html.ActionLink is not working - c#

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.

Related

call get method from a html herf in mvc core C#

Html herf
href="/DeviceMst/Index/"
Controller
[HttpGet]
public IActionResult Index()
{
IEnumerable<DEVICE_MASTER> devices = db.DEVICE_MASTER;
return View(devices);
}
[HttpPost]
public IActionResult Get_status(GetId getId)
{
//logic
return Json("Success");
}
the problem is
Whenever I call get method by clicking "a" tag, it also called the post method even after changing the name of the post method.
I am new to mvc core.

session become null in controller method

I have following controller , in that controller I created session to save IENUMERABLE data-set
[HttpPost]
[ValidateInput(false)]
public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
{
IEnumerable<ProductsPropertiesVM> newmodel = model;
IEnumerable<BrochureTemplateProperties> sample = model.Where.....
Session["TemplateData"] = newmodel;
return View(sample);
}
EDIT:
Create_Brchure View page has href link to call PrintIndex method in same class file
Download ViewAsPdf
this is PrintIndex method
public ActionResult PrintIndex()
{
return new Rotativa.ActionAsPdf("Create_Brochure_PDF") { FileName = "TestActionAsPdf.pdf" };
}
I want to use that session list dataset again in Create_Brochure_PDF controller method,so I created here that method
public ActionResult Create_Brochure_PDF()
{
IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
IEnumerable<BrochureTemplateProperties> samplePDF = newmodel.Where(....
return View(samplePDF);
}
but in above method I'm getting null IEnumerable<ProductsPropertiesVM> newmodel
EDIT:
If I explain this whole scenario
Create_Brochure controller method has a view ,
In that view I have href link to save that Create_Brochure view as
PDF
Once I click that href link I'm calling PrintIndex method so in
that action method again its calling to Create_Brochure_PDF method ,
so I'm getting null object set in Create_Brochure_PDF
I had the same issue some times ago, So I came up with solution ViewasPdf() method in Rotativa library
You can directly call to once You click that href link but you have to create a View for this method that your going to generate as PDF
So here the steps
Create a Action for the View that your Going to Generate as PDF
public ActionResult Create_Brochure_PDF()
{
IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
IEnumerable<BrochureTemplateProperties> samplePDF = newmodel.Where(....
rerurn View();
}
Generate view for that Action method
Replace this line rerurn View(); with following line in Create_Brochure_PDF() method
return new Rotativa.ViewAsPdf("Create_Brochure_PDF") { FileName = "TestActionAsPdf.pdf" };
Now call the Create_Brochure_PDF() method as follows in
Create_Brochure view page
Download ViewAsPdf

Razor form not passing back to controller method

I am trying to pass a textbox's text back to a method in my controller, but the data is not being passed in the parameter
I am genuinely confused, i'm following another example but i'm getting a different result, ie - my method behaving as if no parameter is passed
Code
public ActionResult Index(string searchString)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
var listOfAnimals = db.Animals.ToList();
if (!String.IsNullOrEmpty(searchString))
{
listOfAnimals = listOfAnimals.Where(a => a.AnimalName.ToLower().Contains(searchString.ToLower())).ToList();
}
return View(listOfAnimals);
}
and here is my razor form from my view page
#using(Html.BeginForm("Index", "Home"))
{
#Html.TextBox("searchString")
<input type="submit" id="Index" value="Index" />
}
Can anybody spot why this isn't working?
If more code is needed, please let me know but i think the issue is isolated to here
You code is correct.
Since you didn't add [HttpGet] or [HttpPost] before index method.
This method was called twice.
The first call ran when producing the page with form via url http://server/Home/Index. This call was an http get and searchString mapped from URL was null, which is correct.
The second call ran when you clicked submit button. Correct value would be mapped by MVC correctly.
You need to have 2 Index actions (two methods), one without decorations (GET verb) and another one decorated with HttpPost (POST verb). Basically, when you go to the index page, the GET action is executed. When you submit the form, a POST request is executed and the Index decorated with HttpPost is executed.
// GET
public ActionResult Index() { ... }
// POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
Francisco Goldenstein wrote the recommended way. It means you can have two Index() actions:
// for GET
public ActionResult Index() { ... }
// for POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
However it is possible to have one Index() method for handling both (GET and POST) requests:
public ActionResult Index(string searchString = "")
{
if(!string.IsNullOrEmpty(searchString)
{ /* apply filter rule here */ }
}
You wrote, your code is not working. Do you mean, your action method is not requested after click on the button? Consider to allow empty value Index(string searchString = "")
If your action method is fired but variable is empty, check the name on the View() side. Textbox must not be disabled, of course.

#Html.ActionLink not wotking at all

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?

ASP.NET MVC Url.Action creating Get not Post

I have:
public ActionResult Create(Guid appId)
{
var vm = new CreateViewModel(appId);
return View(vm);
}
[HttpPost]
public ActionResult Create(CreateViewModel vm)
{
// this does some stuff
}
Now, in the View I use this for creating the Form:
#using(Html.BeginForm())
{
}
Standard.
How ever, it produces the wrong HTML:
<form action="/SomeController/Create?appId=414FDS-45F2SF-TEF234">
This is not what I want posted back, I don't want appId what so ever. Just the Create
How do you get around this?
You can use another overload of Html.BeginForm to explicitly specify the action you want:
#using(Html.BeginForm("Create", "SomeController"))
{
}
This will not append anything to the URL by default.

Categories