Html.ActionLink on Post Methods - c#

Is there any way that I can use a html.actionLink to post data?
public ActionResult Test()
{
return View();
}
[HttpPost]
public ActionResult Test(TestModel test)
{
return View(test);
}
and on my view...
#Html.ActionLink("ClickMe","Test", new {Test1 = "actionLink", Test2 = "actionLinkDidThis"}, FormMethod.Post)
Is there any way that this action link could get in the httpPost method and not the httpGet method?

This will render an anchor tag which will produce a get request since it's a regular hyperlink at the end of the day.
You can Make the action method a Get method though by putting a HttpGet attribute on it instead. The MVC model binder will be able to deserialize the Get request into the TestModel object for Get requests as well.
I would use a button and do a form post if you need a post request

Related

Why Should we assign a Http Attribute to every action in controller

I having a question should we assign a http attribute for each action?
Like for example Index Page that doesn't have any action just displaying html we still need to assign a http attribute? Why since there is no retrieving data.
And If I remove Http Attribute for ViewDetail and CreateRecord, the page is still working and no bug what the huge difference for adding and not adding http attribute
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ViewDetail()
{
//.... Get Data Action
return Redirect(Url.Action("Edit","Home"));
}
[HttpPost]
public ActionResult CreateRecord()
{
//.... Create Action
return Redirect(Url.Action("Edit","Home"));
}
There may be some methods in your Controller class that are not HTTP Endpoints.

how do get header that i set in response object

My action method returns a view. It looks like the following.
public public ActionResult Init(string additionalParams)
{
Response.AddHeader("additionalParams", additionalParams);
return View(model);
}
The view has a form. The form is submitted to another action method
[HttpPost]
public ActionResult InitPost(MyModel model)
{
string additionalParams = Request.Headers.Get("additionalParams"); <--- getting null
}
how do i get the additionalParams in the post? Please help.
So on the Init(), I was using Route attribute routing, that makes urls look like this http://wwww.example.com/23/22/... Apparently this caused issues with the length of url. So I switched to using query string, by removing the route attribute routing. Now with this url, I can pass my additionalParams just fine, http://www.example.com?id=222&otherID=222&additionalParams="whatever"

get ajax posted Data From Request body

How to get these ajax posted data from request body ?
search=title&searchType=department+1&X-Requested-With=XMLHttpRequest
You can use [FromBody] attribute to the parameter that you need to get from the body.
Public ActionResult ([FromBody] string param1)
{
return View();
}
Hope this would help.
You can simply get QueryString paramaters as arguments in your Controller:
Public ActionResult Index(string search, string searchType,string XRequestedWith)
{
return View();
}
Of course this way your paramater can not have dashes like X-Requested-With
You can also use Request.QueryString to get QueryString. This way you won't have the restriction above.
If you want to get those ajax posted data from request body the first instruction is not to put those values in query string put those values in form body.Better it would be if you create a modal and use those property to pass in ajax.
Now you would fetch those value in action method like
Public ActionResult YourMethodName([FromBody]YourModel objYourModel)
{
// use objYourModel to fetch your values
return View();
}

MVC Render Action on Post

I have a page that calls another partial view. The page loads fine, but when there is a validation error, it appears to call the post method multiple times.
The code that is causing the issue is here:
<div>
#{Html.RenderAction("ViewUploadedDocs", "TrackingHome", new { number = #Model.Id.ToString() });}
</div>
This should call the following method in the controller.
public ActionResult ViewUploadedDocs(string number)
{
return PartialView();
}
It is not decorated with [HttpGet] or [HttpPost]. The method that keeps getting called is below which is the post method of the page.
[HttpPost]
[MultipleButton(Name = "action", Argument = "Save")]
public ActionResult Edit(EditScreenModelValidation model)
{
if (ModelState.IsValid)
{
return RedirectToAction("UserWorkflows", "Home", new { Area = "Workflow" });
}
return View("Edit", model);
}
I have read on stackoverflow where people have the page calling the post method that they are trying to get, but mine is calling the post method of my main page and not the page that I am trying to get. If I remove the renderAction line in my main page, then the page works correctly, and the action does not call the Edit page in it.
RenderAction invokes the specified child action method and renders the result inline in the parent view (it calls the action). You should use RenderPartial if you need to pass the current ViewDataDictionary object or Partial if you need the specified view to be rendered as an HTML-encoded string, depending on what you're trying to accomplish.

MVC - How to avoid action returning 404 if not POST

I have two actions in a controller:
public ActionResult ReportRequest()
{
return View();
}
[HttpPost]
public ActionResult Report(ReportRequestObj rr, FormCollection formValues)
{
if (Request.HttpMethod != "POST")
return RedirectToAction("ReportRequest");
ReportingEngine re = new ReportingEngine();
Report report = re.GetReport(rr);
return View(report);
}
My problem is, the URL for the 'Report' page gets saved in the browser, and when I click on it, I get a 404, because the request has not been posted.
I put in a handler in to redirect to the report request page however on debugging it just doesn't seem to hit this at all.
Is there any other way I can determine if the request is a post, and if not redirect back to another page?
Thanks
Add action
public ActionResult Report()
{
return RedirectToAction("ReportRequest");
}
or just remove [HttpPost] from action Report
You cannot handle 404 error here because that request will never arrive to your action. You are using an action filter that makes sure that only POST requests arrive to that piece of code.
You should create another action method to respond to GET requests and return your view inside that.
[HttpGet]
public ActionResult Report()
{
return View("ReportRequest");
}

Categories

Resources