简体   繁体   中英

ASP.NET MVC multiple begin forms?

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 ie http://localhost:1338/Tags/InternalTag/TagManagement/D104EBF5-470B-4FAA-9FC7-5391922CCE94/Projects like TagManagement. Please help.

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>
<% } %>

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>
<%}%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM