C# 如何独立提交表格的不同部分?

C# 如何独立提交表格的不同部分?,c#,html,asp.net,asp.net-mvc,razor,C#,Html,Asp.net,Asp.net Mvc,Razor,如图所示,我如何能够独立于表单的其余部分通过“添加标记”来添加标记,也就是说,忽略表单其余部分的验证。同样,反之亦然,我如何“创建”配方,同时忽略添加标签“添加标签”的验证 注意:红线仅用于css调试 视图: @model MVCApp.ViewModels.CreateRecipe @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm("

如图所示,我如何能够独立于表单的其余部分通过“添加标记”来添加标记,也就是说,忽略表单其余部分的验证。同样,反之亦然,我如何“创建”配方,同时忽略添加标签“添加标签”的验证

注意:红线仅用于css调试

视图:

@model MVCApp.ViewModels.CreateRecipe

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm("Create", "Recipe", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Create Recipe</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @*Heading - Select Multiple Tags:*@
        <div class="form-group">
            @Html.LabelFor(model => model.SelectedTagIds, htmlAttributes: new { @class = "control-label", style = "border: 1px solid red;" })
        </div>

        @*Search box - Tag*@
        <input type="text" id="search" name="search" placeholder="Search" style="margin: 10px;width: 165px;border: 1px solid red;" onkeyup="filter()">

        @*List Box - Tag*@
        <div class="form-group" style="border: 1px solid black;">

            @*List box*@
            <div class="col-xs-6 col-sm-4" id="listBoxDiv" style="border: 1px solid red;">
                @Html.ListBoxFor(model => model.SelectedTagIds, Model.TagList, new { @class = "col-xs-12", style = "height:300px; padding:5px;", @onchange = "getSelectedTags(this)" }) @*//OnSelectedIndexChanged*@
                @Html.ValidationMessageFor(model => model.SelectedTagIds, "", new { @class = "text-danger col-xs-12", style = "border: 1px solid red;" })
            </div>

            @*Your tags selected*@
            <div class="col-xs-6 col-sm-8" style="border: 1px solid red;" id="displaySelectedTags">
            </div>
        </div>

        @*EditorFor & Submit - Tag Name*@
        <div class="form-group">
            @Html.LabelFor(model => model.Tag.Name, htmlAttributes: new { @class = "control-label col-md-2", style = "border: 1px solid red;" }) @*sm*@
            <div class="col-md-3" style="border: 1px solid red; padding-right:0;">
                @Html.EditorFor(model => model.Tag.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Tag.Name, "", new { @class = "text-danger" })
            </div>

            @*Submit - Add Tag*@
            <div class="col-md-7" id="addTag" style="border: 1px solid red;">
                <input type="submit" value="Add Tag" class="btn btn-default" style="border: 1px solid red;" />
            </div>
        </div>

        @*File - Upload Image*@
        <div class="form-group">
            <label class="control-label col-md-2" for="file">Upload Image:</label>
            <div class="col-md-10">
                <input type="file" name="file" id="file" style="width:100%" />
            </div>
        </div>

        @*EditorFor - Recipe Title*@
        <div class="form-group">
            @Html.LabelFor(model => model.Recipe.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Recipe.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Recipe.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        @*EditorFor - Recipe Instructions*@
        <div class="form-group">
            @Html.LabelFor(model => model.Recipe.Instructions, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Recipe.Instructions, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Recipe.Instructions, "", new { @class = "text-danger" })
            </div>
        </div>

        @*Submit - Create Recipe*@
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
模型

    public class CreateRecipe
    {
        // A Recipe object
        public Recipe Recipe { get; set; }

        [Required(ErrorMessage = "At least one Tag is required.")]
        [Display(Name = "Select multiple tags:")]
        public int[] SelectedTagIds { get; set; }

        public IEnumerable<SelectListItem> TagList { get; set; }

        // A tag object
        public Tag Tag { get; set; }
    }
公共类CreateRecipe
{
//配方对象
公共配方{get;set;}
[必需(ErrorMessage=“至少需要一个标记。”)]
[显示(Name=“选择多个标记:”)]
public int[]selectedtagds{get;set;}
公共IEnumerable标记列表{get;set;}
//标记对象
公共标记{get;set;}
}

谢谢

您可以在同一页面上有多个表单,指向不同的操作。如果您需要访问两个模型,那么页面的视图模型应该包含这两个模型,就像您已经拥有它们一样

控制器将有两个操作,一个用于添加配方,一个用于添加标签,或者它可以指向两个不同的控制器

视图也可以有两种形式。在下面的示例中,它们命中不同的控制器,但您也可以在同一控制器上命中不同的操作

@using (Html.BeginForm("Create", "Recipe", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
.....
}

@using (Html.BeginForm("Create", "Tag", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
.....
}

如果您这样做,需要记住的一点是,两个操作可能同时被调用,因此您需要在代码中找到一种方法来确定您正在执行哪一个操作。

您的问题太广泛了。有太多的方法可以满足你的要求。阅读关于单页应用程序(SPA)以及如何使用ASP.NET构建SPA。您不限于每页一个表单。只需添加另一个具有不同操作的表单。@DonO这是我最初的猜测,但“添加标签”部分/表单将在“创建”配方表单内?不,它将在外部。我将尝试在下面的示例中给出一个简短的答案。@C.AugustoProiete,因为SPA正在动态重写当前网页,与浏览器加载整个新页面的默认方法不同,我仍然不知道从哪里开始专门研究,以便能够只提交表单的一部分,而忽略对其余部分的验证。在我现在看到的视图中,“添加标签”部分位于“创建”配方之间。(列表框用于“创建”配方)。我看不出如何像您所示的那样将一个表单放在另一个表单上,但将“添加标签”部分楔入创建配方表单之间,就像它当前显示在视图上一样。我只能看到它在视觉上工作,如果一个表单的部分/输入堆叠在另一个表单上,并且没有视觉上的混合。
@using (Html.BeginForm("Create", "Recipe", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
.....
}

@using (Html.BeginForm("Create", "Tag", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
.....
}