1

That's my URL:

http://localhost:7071/TODO?page=1&codcat=56

It returns me a simple form , it is my form of research:

       <div class="col-md-8">
            @using (Html.BeginForm("Index", "TODO", FormMethod.Get))
            {
                <p>
                    Find in ...: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
                    <input type="submit" value="Search" />
                </p>
            }
            <br />
        </div>

The Search method in TODO control is :

        public async Task<ActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page, int? codcat)
        {

            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var todos = from v in db.TODOs 
                         join cv in db.TODO_CATEGORIA on v.ID equals cv.ID_TODO
                         where cv.ID_CATEGORIA == codcat select v;
            
            if (!String.IsNullOrEmpty(searchString))
            {
                todos = todos.Where(s => s.TODO_TITLE.Contains(searchString)
                                    || s.DESCRIPTION.Contains(searchString) 
                    );
            }


            switch (sortOrder)
            {
                case "title_desc":
                    todos = todos.OrderByDescending(s => s.TODO_TITLE);
                    break;
                case "Date":
                    todos = todos.OrderBy(s => s.DATE);
                    break;
                case "date_desc":
                    todos = todos.OrderByDescending(s => s.DATE);
                    break;
                default:
                    todos = todos.OrderBy(s => s.TODO_TITLE);
                    break;
            }
            int pageSize = 21;
            int pageNumber = (page ?? 1);
            return View(todos.ToPagedList(pageNumber, pageSize)); 

        }

The problem is that the codcat parameter is sent by the form as null.

How could I do to the form keeping the value of the parameter codcat and send the parameter research.

1 Answer 1

3

The one way is to use hidden fields.

<input type="hidden" name="codcat" value="@Request.QueryString["codcat"]" />

When submitting a form the POST request is sent to the server, and all your GET request's parameters are left behind.

OR to use this:

public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method);

OR this overload of BeginForm:

public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.