-1

I'm having a real problem trying to articulate this seemly simple problem.

I have a single view that contains a FORM with a few search fields at the top of the view and the results of that search get shown on the same view after submitting the form.

I have a single HTTPGET controller method that takes the form fields as parameters and IF it was submitted by a user it will pass the model back to the view with the results to be shown and pre-populate the search form with what they filled out.

How can I tell if the page was loaded with default parameters vs. someone actually submitted the form.

What's the best way to accomplish this?

user3953989
  • 1,844
  • 3
  • 25
  • 56

2 Answers2

0

If I am understanding your question correctly then I think you need to consider the HttpGet attribute:

https://msdn.microsoft.com/en-us/library/system.web.mvc.httpgetattribute(v=vs.118).aspx

and the HttpPost attribute:

https://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattribute(v=vs.118).aspx

Lets say you have a create method. The Http method would look like this:

[HttpGet]
public ActionResult Create()
{

}

and the post method would look like this:

[HttpPost]
public ActionResult Create(Person p)
{
//Logic to insert p into database.  Could call an application service/repository to do this
}
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • This is the same answer he got to his [other question](https://stackoverflow.com/questions/46081518/why-is-my-model-failing-validation) and didn't like it. – Fran Sep 06 '17 at 20:05
  • @Fran, I don't understand what you mean. Can you clarify? Thanks. – w0051977 Sep 06 '17 at 20:11
  • See the question I linked to. The OP is trying to do too many things with one action and it's causing them grief. He's got an answer on the question that tell him exactly what you are suggesting. – Fran Sep 06 '17 at 20:27
  • Ok. I did not see that. I will vote to close this question. – w0051977 Sep 06 '17 at 20:30
0

RedirectToAction solve the problem.

you can go back to the get method after submited data and populate the view with default values

[HttpGet]
public ActionResult Create()
{
   // fill model to default data
   return view(model);
}

[HttpPost]
public ActionResult Create(Person p)
{     
   //do your stuff save data 
   return RedirectToAction("Create");      
}

or

[HttpPost]
public ActionResult Create(Person p)
{     
   if(...)
   { 
     //do your stuff  any logic
     return RedirectToAction("Create");
   }

   //do your stuff 

     return view(...);
}