Implementing forms in ASP.net MVC

I have a simple form on the browse page implemented as a user control that looks something like this:

<%=Html.BeginForm("List", "Building", FormMethod.Post) %>

//several fields go here

<%Html.EndForm(); %>

There are two problems that I would like to solve, firstly, I would like the controller method to get this in order to accept a parameter like a user control. The goal is not to put all the form fields in the parameter list for the method. Now the controller method is as follows:

[AcceptVerbs("Post")]
    public ActionResult List(string capacityAmount)
    {
        ProfilerDataDataContext context = new ProfilerDataDataContext();
        IEnumerable<Building> result = context.Buildings.OrderBy(p => p.SchoolName);
        ViewData["Boroughs"] = new SelectList(Boroughs.BoroughsDropDown());

        return View(result);
    }

The remaining fields in the form will be used to search by type of building.

The message form is excellent, I can search for capacity as you would expect, but I can smell the ugliness ahead when I add parameters to the search.

-, , BeginForm, "System.Web.Mvc.Form". ?

+3
3

1) FormCollection:

public ActionResult List(FormCollection searchQuery)

FormCollection / .

2) "=" BeginForm:

<% Html.BeginForm("List", "Building", FormMethod.Post) %>

, , um... :

<% using (Html.BeginForm("List", "Building", FormMethod.Post)) { %>
<% } %>
+6

, html :

<%=Html.TextBox("building.FieldNumber1")%>
<%=Html.TextBox("building.FieldNumber2")%>

, :

public ActionResult List(Building building)
{
   ...
   var1 = building.FieldNumber1;
   var2 = building.FieldNumber2;
   ...
}

:

public ActionResult List()
{
    //some code here
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(Building building)
{
   ...
   var1 = building.FieldNumber1;
   var2 = building.FieldNumber2;
   ...
}
0

if someone is skeptical about the entire “use” pattern with Html.BeginForm - understands that the IDE is smart enough to match opening '{'with ending '}', which makes it very easy to see where your form starts and ends.

Also <% Html.EndForm (); %> requires a semicolon, which I'm not sure what I like :)

0
source

Source: https://habr.com/ru/post/1702303/


All Articles