New to MVC - how to get route to ignore httppost actionresult RRS feed

  • Question

  • The MVC app I'm trying to create is a simple single form that the user will fill out and click a button to save the data. Initially the "blank" form needs to have a ddl that is filled from SQL (people list), and the selected item of the ddl needs to be set to the logged on person.  I also need it to show the error message for required fields if they are not filled in.

    I have a controller that I have the following actionresults: Index(), Create(), [HttpPost]Create(MyClass, myClass).

    I started with a view called Index, and code to load the ddl and set it to the logged on person - this all worked. 

    My issue is I tried to add a button to THAT view to run the Create(MyClass myClass) actionresult.  When the button is clicked, I get an error that it's looking for a view called Create.  So I created a view called Create that looks the same as the Index view since all the fields are the same. 

    When I click the Create button (input type submit) on the Index view now, I don't have an object in the Create view that can be saved since I wasn't in this view to start with so I'm getting an error on the drop down list that should've been loaded and set. 

    I tried cutting out the Index view altogether, and putting my Index (Load and set the ddl) code into Create(), then changed the routeconfig to <Controller>/Create, but this directs the app to the HttpPost Create(MyClass, myClass) ActionResult right away.  If I comment out the HttpPost Create actionresult, I am correctly routed to the Create() ActionResult.

    I'm thinking I need to have the route config set in such a way that it goes to the Create() ActionResult and ignores the HttpPost Create(MyClass, myClass) ActionResult when the page loads.  I'm just not sure how to do this? 

    Or if I'm even going about this the right way?  This is so different from webforms.

    So, to get a page to come up with blank text boxes and 1 loaded and set ddl when the page loads - let the user fill out this form - show a "required field" error for any fields that are not filled out (I decorated the class with [Required]) and then Save the data when everything IS complete, and then send the user to a "Thank you for your input" page... how can I do this??

    Any examples/links/help of any kind would be great.  Thanks!

    • Moved by CoolDadTx Wednesday, August 22, 2018 3:24 PM ASP.NET related
    Wednesday, August 22, 2018 1:50 PM


  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by KimFromPA Monday, August 27, 2018 1:29 PM
    Wednesday, August 22, 2018 3:24 PM