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;
...
}
}









share|improve this question






















  • 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















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;
...
}
}









share|improve this question






















  • 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













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;
...
}
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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 :)






share|improve this answer





















  • 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


















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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 :)






    share|improve this answer





















    • 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















    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 :)






    share|improve this answer





















    • 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













    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 :)






    share|improve this answer












    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 :)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 19:57

























        answered Nov 11 at 19:51









        Erik Funkenbusch

        79.4k24156251




        79.4k24156251






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine