Asp.net mvc 4 MvC4使用同一行上的2个提交表单

Asp.net mvc 4 MvC4使用同一行上的2个提交表单,asp.net-mvc-4,submit,row,Asp.net Mvc 4,Submit,Row,这就是代码,我有两个提交按钮,当我按“开始”时,我希望它发送日期时间。现在到开始行,当我按“停止”时,我希望它发送停止日期时间。现在到列,这应该发生在同一行。当我再次按下Start时,它会生成一个新的ID 2,等等。在第二行打印开始日期 示例ID 1:Start 2013-11-15 05:12荡妇:2013-11-15 05:15 你好,帕特里克 @using (Html.BeginForm()) { @Html.AntiForgeryToken()

这就是代码,我有两个提交按钮,当我按“开始”时,我希望它发送日期时间。现在到开始行,当我按“停止”时,我希望它发送停止日期时间。现在到列,这应该发生在同一行。当我再次按下Start时,它会生成一个新的ID 2,等等。在第二行打印开始日期

示例ID 1:Start 2013-11-15 05:12荡妇:2013-11-15 05:15

你好,帕特里克

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.Start)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now })
            @Html.ValidationMessageFor(model => model.Start)
        </div>
         <p>
            <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" />
        </p>

    }
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.Slut)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now })
            @Html.ValidationMessageFor(model => model.Slut)
        </div>
        <p>
            <input type="submit" name="@Html.NameFor(x => x.Command)"  value="Stop" />
        </p>
    }
</fieldset>

            <div class="editor-label">
                @Html.LabelFor(model => model.Slut)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Slut)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
模型

public class ArbetsTider
{
    public int ID { get; set; }
    public DateTime Start { get; set; }
    public DateTime Slut { get; set; }

}

public class TiderDBContext : DbContext
{
    public DbSet<ArbetsTider> Tider { get; set; }
}
公共类仲裁程序
{
公共int ID{get;set;}
公共日期时间开始{get;set;}
公共日期时间荡妇{get;set;}
}
公共类TiderDBContext:DbContext
{
公共DbSet Tider{get;set;}
}

默认情况下,HTML不能很好地处理多个表单

例如,不能有嵌套表单:

<form action="/GreatController/DecentAction">
    <input type="submit" value="Start" />

    <form action="/GoodController/ModerateAction">
        <input type="submit" value="Stop" />
    </form>

</form>

即使使用两个不同的按钮,也不能让表单使用相同的数据(仅使用HTML)执行两个不同的操作

有几种方法可以解决这个问题

  • Javascript-您可以使用Javascript以各种方式帮助您。它可以创建一个新的隐藏表单,创建隐藏字段,或者在您使用特定按钮提交表单时,只需更改表单的
    操作
    。可能性很多
  • 重新思考-问问自己是否可以通过不同的方式实现相同的功能
  • 将表单拆分-能否将表单拆分为两个表单?请记住,表单只需要包含执行操作所需的数据(即输入字段),而不需要包装大量不相关的HTML

  • 像这样的怎么样

    <fieldset>
        <legend>ArbetsTider</legend>
    
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Start)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Start)
            </div>
            <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" />
            </p>
        }
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Slut)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Slut)
            </div>
            <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)"  value="Create" />
            </p>
        }
    </fieldset>
    
    别开玩笑了:

        @using (Html.BeginForm("Start"))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Start)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Start)
            </div>
             <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" />
            </p>
    
        }
    
        @using (Html.BeginForm("Stop"))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Slut)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Slut)
            </div>
            <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)"  value="Stop" />
            </p>
        }
    
    注意:通常您会在中间添加一个额外的DAL层,比如存储库。我这样做并不是为了让事情简洁

    您所要做的就是在表单中定义它应该指向的操作方法。
    你的问题有点不清楚(你说“在同一行上”是什么意思?),因此根据你的用例,你可能必须在每个表单中添加一个隐藏字段,以确定你试图更新的模型的ID。

    模型是如何被标记为荡妇的?!哈哈,我来自瑞典。。荡妇=停止:)我不确定我是否理解你的答案,我要求你直接在这里。对同一行但不同列使用两个提交按钮并不像我想的那么简单?好的,我会尝试让你明白:)我有3个不同列ID,开始,停止。当我按下“开始”-提交按钮时,我希望它将开始日期发送到开始列,当我按下“停止”-提交按钮时,我希望它将停止日期发送到停止列。我在更新中也添加了这个模型。@PatrikChristoffersson:请看我的编辑,应该就是这样。控制器应该看起来怎么样?
    [HttpPost]
    public ActionResult MultipleCommand(string Command)
    
    
        if (model.Command == "Start")
        {
            //TO DO : Submit button for first row
        }
        else if (model.Command == "Stop");
        {
            //TO DO : Submit to second row
        }
    }
    
        @using (Html.BeginForm("Start"))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Start)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Start, new { style = "display: none;", @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Start)
            </div>
             <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)" value="Start" formaction="/tider/create" />
            </p>
    
        }
    
        @using (Html.BeginForm("Stop"))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <div class="editor-label">
                @Html.LabelFor(model => model.Slut)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Slut, new { @Value = @DateTime.Now })
                @Html.ValidationMessageFor(model => model.Slut)
            </div>
            <p>
                <input type="submit" name="@Html.NameFor(x => x.Command)"  value="Stop" />
            </p>
        }
    
    [HttpPost]
    public ActionResult Start(IDontKnowYourModel model){
     using(var context = new TiderDBContext()) {
       context.Tider.FirstOrDefault(x => x.ID == model.ID).Start = model.Start;
       context.SaveChanges();
     }
     return View("Index");
    }
    
    [HttpPost]
    public ActionResult Stop(IDontKnowYourModel model){
     using(var context = new TiderDBContext()) {
       context.Tider.FirstOrDefault(x => x.ID == model.ID).Slut = model.Slut;
       context.SaveChanges();
     }
     return View("Index");
    }