3

Hi I am using multiple begin forms. I am not sure if this is a good Idea. This is my code.

<% using (Html.BeginForm("TagManagement", "InternalTag",FormMethod.Post))
   {%>
   <%: Html.AntiForgeryToken() %>

    <table>         
        <tr>
            <td><%:Html.Label("Type") %></td>
            <td style="text-align:left"><%:Html.DropDownList("ObjectType",ViewData["TagObjects"] as IEnumerable<SelectListItem>)%></td>
        </tr>
        <tr>
            <td><%:Html.Label("Search ID")%></td>
            <td style="text-align:left"><%:Html.TextBox("ObjectID2")%></td>
        </tr>
        <tr>
            <td></td>
            <td><input id="Search" type="submit" value="Search" />&nbsp;&nbsp;&nbsp;<input id="Create" type="button" value="Add to New Tag" onclick="AddTagElements()" /></td>
        </tr>
    </table>
    <br />

<% } %>

<% using (Html.BeginForm("CreateTag","InternalTag",FormMethod.Post)) { %>
<%: Html.AntiForgeryToken() %>
<div id="AddTag">
      <input id='SaveTag' type='submit' value='Save' style='width: 125px' />
</div>
<% } %>

routing code

"Tags/{controller}/{action}/{id}/{type}",
new { action = "Index", id = UrlParameter.Optional, type = UrlParameter.Optional }

my controller methods:

public ActionResult TagManagement(FormCollection FC,Guid? ID,InternalTagRef_Types? Type)
public ActionResult CreateTag(FormCollection FC, Guid ID, InternalTagRef_Types Type)

when I click on search TagManagement Actions parameters get properly populated. When I click on SaveTag it tries call the CreateTag action but cannot because it is missing ID and Type. I get the error The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Guid'. It should get these values from the URL i.e. http://localhost:1338/Tags/InternalTag/TagManagement/D104EBF5-470B-4FAA-9FC7-5391922CCE94/Projects like TagManagement. Please help.

1
  • Yes, having multiple forms per page is an excellent idea - the one page per form restriction is one of the major annoyances of WebForms IMO Commented Dec 14, 2010 at 10:30

2 Answers 2

3

Your second form contains no input fields (other than a submit button) so when the form is submitted nothing is sent to the server and the controller action has nothing to bind from. So you will need to include text fields or hidden fields as you did in your first form.


UPDATE:

You cold include route parameters in the generated action:

<% using (Html.BeginForm(
        "CreateTag", "InternalTag", 
        new { id = RouteData.Values["id"], type = RouteData.Values["type"] }, 
        FormMethod.Post)) { %>
    <%: Html.AntiForgeryToken() %>
    <div id="AddTag">
        <input id='SaveTag' type='submit' value='Save' style='width: 125px' />
    </div>
<% } %>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi sorry that my question was not that descriptive I edited. The ID and Type should be retrieved from the U.R.L
@user351709, I understand, see my update about how you could include route parameters in the generated form action. RouteData.Values["xx"] will use the routing engine to fetch the value of the parameter from the current url.
1

I would suggest including a hidden field in this form which will have your tags, thus when submitting the form your tags will be available. Secondly, and equally important

<input id="Create" type="button" value="Add to New Tag" onclick="AddTagElements()" />

the AddTagElements javascript function needs to update the value of the hidden field with the new tag(s).

<% using (Html.BeginForm("CreateTag","InternalTag",FormMethod.Post)){%>
    <%: Html.AntiForgeryToken() %>
    <div id="AddTag">
        <input id="tags" type="hidden" value=""/>
        <input id='SaveTag' type='submit' value='Save' style='width: 125px' />
    </div>
<%}%>

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.