0

I have a view that is bound to the model (EF using ASP.NET MVC 5) and there are three submit buttons to do 3 different things. I was having trouble debugging my logic based on Request.Form["button"] and found that the Request.Form.Keys[2] value contained the string that I was testing for with the 3 different buttons, so I started to use that value directly in my controller logic here:

// This gets post from 3 actions
// 1. Filter Button to filter the index to just the selected project
// 2. All Button to clear the project filter and show all
// 3. Create New button to create a lesson with the project
//    selected in the dropdown (even if not filtered)

var sRequest = Request.Form.Keys[2];

if (sRequest == "CreateNew")
{
    return RedirectToAction("Create", new { pid = lvm.ProjectFilter });
}

var lessons = db.Lessons
                .Include(l => l.Division)
                .Include(l => l.Project)
                .Include(l => l.People);

if (lvm.ProjectFilter != null && sRequest == "Filter") //user pressed the filter button
{
    lessons = lessons.Where(l => l.Project.ID == lvm.ProjectFilter);
}
else if (sRequest == "ClearFilter") 
    lvm.ProjectFilter = null;

This works, but I cannot find any documentation that lets me understand what changes will break my logic.

How is the count of Keys derived based on my view structure? It appears that it is a coincidence that I have 3 submit buttons and keys count is 3, but I'm not convinced that my logic will hold up. I did try adding a string submit parameter to the controller post method, but this resulted in the view model not being properly returned and the value of submit was always null.

Here is the view markup:

<div class="col-md-10">
    <input type="submit" value="Add New Lesson" id="CreateNew" name="CreateNew" class="btn btn-default" />
</div>
<div>
    <input type="submit" value="Filter" name="Filter" id="Filter" style="float:left" class="btn btn-default" />
    <input type="submit" value="All" name="ClearFilter" id="ClearFilter" style="float:right" class="btn btn-default" />
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul Gibson
  • 622
  • 1
  • 9
  • 23

1 Answers1

0

i hope everything is alright. I came across a similar situation and this post helped me.

It's works for me but I thought it was easier to do back in the days of JQuery hahahhaha

I hope this helps.

Diego Lobo
  • 156
  • 7