Why is MVC always going to [HttpPost] method?
up vote
0
down vote
favorite
I have a link which uses a query string, and it's always going to the [HttpPost] method instead of the [HttpGet] method.
The error I'm getting is a NullReferenceException on TempData["surveytype"], so I know it's going to Post instead of Get. I have no idea why though.
I found a couple of similar questions here but nothing that resolved my problem. I thought maybe MVC was interpreting this as a form submission and sending to HttpPost because I'm styling the link as a "btn btn-primary" class, but removing that changed nothing.
My link:
<a href="../Responses/Create?SurveyId=@item.id" class="btn btn-primary">Start Response</a>
Controller:
[HttpGet]
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = ...)] Response response)
{
if (ModelState.IsValid)
{
response.Userid = User.Identity.GetUserId();
response.Id = Guid.NewGuid();
db.response.Add(response);
db.SaveChanges();
TempData["ResponseId"] = response.Id;
int? surveyid = response.SurveyId;
var surveytype = db.surveys.Find(surveyid).surveytype;
TempData["surveytype"] = surveytype;
...
}
}
c# asp.net-mvc
add a comment |
up vote
0
down vote
favorite
I have a link which uses a query string, and it's always going to the [HttpPost] method instead of the [HttpGet] method.
The error I'm getting is a NullReferenceException on TempData["surveytype"], so I know it's going to Post instead of Get. I have no idea why though.
I found a couple of similar questions here but nothing that resolved my problem. I thought maybe MVC was interpreting this as a form submission and sending to HttpPost because I'm styling the link as a "btn btn-primary" class, but removing that changed nothing.
My link:
<a href="../Responses/Create?SurveyId=@item.id" class="btn btn-primary">Start Response</a>
Controller:
[HttpGet]
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = ...)] Response response)
{
if (ModelState.IsValid)
{
response.Userid = User.Identity.GetUserId();
response.Id = Guid.NewGuid();
db.response.Add(response);
db.SaveChanges();
TempData["ResponseId"] = response.Id;
int? surveyid = response.SurveyId;
var surveytype = db.surveys.Find(surveyid).surveytype;
TempData["surveytype"] = surveytype;
...
}
}
c# asp.net-mvc
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a link which uses a query string, and it's always going to the [HttpPost] method instead of the [HttpGet] method.
The error I'm getting is a NullReferenceException on TempData["surveytype"], so I know it's going to Post instead of Get. I have no idea why though.
I found a couple of similar questions here but nothing that resolved my problem. I thought maybe MVC was interpreting this as a form submission and sending to HttpPost because I'm styling the link as a "btn btn-primary" class, but removing that changed nothing.
My link:
<a href="../Responses/Create?SurveyId=@item.id" class="btn btn-primary">Start Response</a>
Controller:
[HttpGet]
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = ...)] Response response)
{
if (ModelState.IsValid)
{
response.Userid = User.Identity.GetUserId();
response.Id = Guid.NewGuid();
db.response.Add(response);
db.SaveChanges();
TempData["ResponseId"] = response.Id;
int? surveyid = response.SurveyId;
var surveytype = db.surveys.Find(surveyid).surveytype;
TempData["surveytype"] = surveytype;
...
}
}
c# asp.net-mvc
I have a link which uses a query string, and it's always going to the [HttpPost] method instead of the [HttpGet] method.
The error I'm getting is a NullReferenceException on TempData["surveytype"], so I know it's going to Post instead of Get. I have no idea why though.
I found a couple of similar questions here but nothing that resolved my problem. I thought maybe MVC was interpreting this as a form submission and sending to HttpPost because I'm styling the link as a "btn btn-primary" class, but removing that changed nothing.
My link:
<a href="../Responses/Create?SurveyId=@item.id" class="btn btn-primary">Start Response</a>
Controller:
[HttpGet]
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = ...)] Response response)
{
if (ModelState.IsValid)
{
response.Userid = User.Identity.GetUserId();
response.Id = Guid.NewGuid();
db.response.Add(response);
db.SaveChanges();
TempData["ResponseId"] = response.Id;
int? surveyid = response.SurveyId;
var surveytype = db.surveys.Find(surveyid).surveytype;
TempData["surveytype"] = surveytype;
...
}
}
c# asp.net-mvc
c# asp.net-mvc
asked Nov 11 at 17:49
extensionhelp
2206
2206
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54
add a comment |
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I think your [HttpGet]
makes the routing to confuse - Remove it
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
Now based on the action name the route will take care either it's a get or post - just a solution try it out and let me know if you face same issue
Thanks - Happy coding :)
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now
– Rahul
Nov 11 at 19:18
add a comment |
up vote
0
down vote
For starters, you shouldn't manually create links like that. It's very error prone, and you run into situations like this often. You should use the helpers to create the link. For example:
@Html.ActionLink("Start Responses", "Create", "Controllername", new { SurveyId = item.id }, new { @class = "btn btn-primary" })
Next, you should make sure you don't have any custom routing that might be interfering, either with Attribute based routing or by using MapRoute.
The biggest reason to use the helpers is that MVC can access pages from different actual url's, and hard coding a path like that is almost impossible to get right.
For instance, let's say you can access a page at http://exmample.com, http://example.com/Home or http://example.com/Home/Index.
Using a hard coded "../whatever" means that this will translate to the corresponding url's http://example.com/../whatever (obviously not what you want), http://example.com/Home/../Whatever (might be what you want, might not), or http://example.com/Home/Index/../Whatever (this is probably what you want want, but it won't get there unless the users's browser url has this third url to access it).
If you must hard code a link, then you should always use a full root relative url link ("/Home/whatever") instead of relative ("../whatever"), but even that has issues.. what happens if you decide to move your site into a subdirectory of your site, now all your hard coded urls are wrong.
Using Url helpers is always the best way to go in MVC.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think your [HttpGet]
makes the routing to confuse - Remove it
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
Now based on the action name the route will take care either it's a get or post - just a solution try it out and let me know if you face same issue
Thanks - Happy coding :)
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now
– Rahul
Nov 11 at 19:18
add a comment |
up vote
0
down vote
I think your [HttpGet]
makes the routing to confuse - Remove it
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
Now based on the action name the route will take care either it's a get or post - just a solution try it out and let me know if you face same issue
Thanks - Happy coding :)
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now
– Rahul
Nov 11 at 19:18
add a comment |
up vote
0
down vote
up vote
0
down vote
I think your [HttpGet]
makes the routing to confuse - Remove it
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
Now based on the action name the route will take care either it's a get or post - just a solution try it out and let me know if you face same issue
Thanks - Happy coding :)
I think your [HttpGet]
makes the routing to confuse - Remove it
public ActionResult Create(int SurveyId)
{
TempData["SurveyId"] = SurveyId;
return View();
}
Now based on the action name the route will take care either it's a get or post - just a solution try it out and let me know if you face same issue
Thanks - Happy coding :)
answered Nov 11 at 19:08
Rahul
9381315
9381315
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now
– Rahul
Nov 11 at 19:18
add a comment |
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now
– Rahul
Nov 11 at 19:18
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
I am getting the same error after removing [HttpGet].
– extensionhelp
Nov 11 at 19:15
try to debug and check which method is called - if nothing works out you can add
[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now– Rahul
Nov 11 at 19:18
try to debug and check which method is called - if nothing works out you can add
[Route([controller]/anyName)]
attribute and change your route names - not a traditional way but as a work around for now– Rahul
Nov 11 at 19:18
add a comment |
up vote
0
down vote
For starters, you shouldn't manually create links like that. It's very error prone, and you run into situations like this often. You should use the helpers to create the link. For example:
@Html.ActionLink("Start Responses", "Create", "Controllername", new { SurveyId = item.id }, new { @class = "btn btn-primary" })
Next, you should make sure you don't have any custom routing that might be interfering, either with Attribute based routing or by using MapRoute.
The biggest reason to use the helpers is that MVC can access pages from different actual url's, and hard coding a path like that is almost impossible to get right.
For instance, let's say you can access a page at http://exmample.com, http://example.com/Home or http://example.com/Home/Index.
Using a hard coded "../whatever" means that this will translate to the corresponding url's http://example.com/../whatever (obviously not what you want), http://example.com/Home/../Whatever (might be what you want, might not), or http://example.com/Home/Index/../Whatever (this is probably what you want want, but it won't get there unless the users's browser url has this third url to access it).
If you must hard code a link, then you should always use a full root relative url link ("/Home/whatever") instead of relative ("../whatever"), but even that has issues.. what happens if you decide to move your site into a subdirectory of your site, now all your hard coded urls are wrong.
Using Url helpers is always the best way to go in MVC.
add a comment |
up vote
0
down vote
For starters, you shouldn't manually create links like that. It's very error prone, and you run into situations like this often. You should use the helpers to create the link. For example:
@Html.ActionLink("Start Responses", "Create", "Controllername", new { SurveyId = item.id }, new { @class = "btn btn-primary" })
Next, you should make sure you don't have any custom routing that might be interfering, either with Attribute based routing or by using MapRoute.
The biggest reason to use the helpers is that MVC can access pages from different actual url's, and hard coding a path like that is almost impossible to get right.
For instance, let's say you can access a page at http://exmample.com, http://example.com/Home or http://example.com/Home/Index.
Using a hard coded "../whatever" means that this will translate to the corresponding url's http://example.com/../whatever (obviously not what you want), http://example.com/Home/../Whatever (might be what you want, might not), or http://example.com/Home/Index/../Whatever (this is probably what you want want, but it won't get there unless the users's browser url has this third url to access it).
If you must hard code a link, then you should always use a full root relative url link ("/Home/whatever") instead of relative ("../whatever"), but even that has issues.. what happens if you decide to move your site into a subdirectory of your site, now all your hard coded urls are wrong.
Using Url helpers is always the best way to go in MVC.
add a comment |
up vote
0
down vote
up vote
0
down vote
For starters, you shouldn't manually create links like that. It's very error prone, and you run into situations like this often. You should use the helpers to create the link. For example:
@Html.ActionLink("Start Responses", "Create", "Controllername", new { SurveyId = item.id }, new { @class = "btn btn-primary" })
Next, you should make sure you don't have any custom routing that might be interfering, either with Attribute based routing or by using MapRoute.
The biggest reason to use the helpers is that MVC can access pages from different actual url's, and hard coding a path like that is almost impossible to get right.
For instance, let's say you can access a page at http://exmample.com, http://example.com/Home or http://example.com/Home/Index.
Using a hard coded "../whatever" means that this will translate to the corresponding url's http://example.com/../whatever (obviously not what you want), http://example.com/Home/../Whatever (might be what you want, might not), or http://example.com/Home/Index/../Whatever (this is probably what you want want, but it won't get there unless the users's browser url has this third url to access it).
If you must hard code a link, then you should always use a full root relative url link ("/Home/whatever") instead of relative ("../whatever"), but even that has issues.. what happens if you decide to move your site into a subdirectory of your site, now all your hard coded urls are wrong.
Using Url helpers is always the best way to go in MVC.
For starters, you shouldn't manually create links like that. It's very error prone, and you run into situations like this often. You should use the helpers to create the link. For example:
@Html.ActionLink("Start Responses", "Create", "Controllername", new { SurveyId = item.id }, new { @class = "btn btn-primary" })
Next, you should make sure you don't have any custom routing that might be interfering, either with Attribute based routing or by using MapRoute.
The biggest reason to use the helpers is that MVC can access pages from different actual url's, and hard coding a path like that is almost impossible to get right.
For instance, let's say you can access a page at http://exmample.com, http://example.com/Home or http://example.com/Home/Index.
Using a hard coded "../whatever" means that this will translate to the corresponding url's http://example.com/../whatever (obviously not what you want), http://example.com/Home/../Whatever (might be what you want, might not), or http://example.com/Home/Index/../Whatever (this is probably what you want want, but it won't get there unless the users's browser url has this third url to access it).
If you must hard code a link, then you should always use a full root relative url link ("/Home/whatever") instead of relative ("../whatever"), but even that has issues.. what happens if you decide to move your site into a subdirectory of your site, now all your hard coded urls are wrong.
Using Url helpers is always the best way to go in MVC.
edited Nov 11 at 19:57
answered Nov 11 at 19:51
Erik Funkenbusch
79.4k24156251
79.4k24156251
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251512%2fwhy-is-mvc-always-going-to-httppost-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you still get the error if you comment the first line in GET?
– Aman B
Nov 11 at 18:00
Yes. I wasn't sure if you meant the [HttpGet] or the TempData line, but I commented both and still got the same error.
– extensionhelp
Nov 11 at 18:07
It would be awesome if you could provide a Minimal, Complete, and Verifiable example, including your route setup code. Do this in a completely new project, so you just need to share the minimal code necessary.
– mjwills
Nov 11 at 20:54