0

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

Example ID 1 : Start 2013-11-15 05:12 Slut : 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>
    }

控制器 { 公共类 TiderController :控制器 { 私有 TiderDBContext db = new TiderDBContext();

        //
        // GET: /Tider/

        public ActionResult Index()
        {
            return View(db.Tider.ToList());
        }

        //
        // GET: /Tider/Details/5

        public ActionResult Details(int id = 0)
        {
            ArbetsTider arbetstider = db.Tider.Find(id);
            if (arbetstider == null)
            {
                return HttpNotFound();
            }
            return View(arbetstider);
        }

        //
        // GET: /Tider/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Tider/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ArbetsTider arbetstider)
        {
            if (ModelState.IsValid)
            {
                db.Tider.Add(arbetstider);
                db.SaveChanges();

            }

            return View(arbetstider);
        }

        //
        // GET: /Tider/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ArbetsTider arbetstider = db.Tider.Find(id);
            if (arbetstider == null)
            {
                return HttpNotFound();
            }
            return View(arbetstider);
        }

        //
        // POST: /Tider/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ArbetsTider arbetstider)
        {
            if (ModelState.IsValid)
            {
                db.Entry(arbetstider).State = EntityState.Modified;

               return RedirectToAction("Index");
            }
            return View(arbetstider);
        }

        //
        // GET: /Tider/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ArbetsTider arbetstider = db.Tider.Find(id);
            if (arbetstider == null)
            {
                return HttpNotFound();
            }
            return View(arbetstider);
        }

        //
        // POST: /Tider/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ArbetsTider arbetstider = db.Tider.Find(id);
            db.Tider.Remove(arbetstider);
            db.SaveChanges();
           return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [HttpPost]
        public ActionResult Start(ArbetsTider 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(ArbetsTider model)
        {
            using (var context = new TiderDBContext())
            {
                context.Tider.FirstOrDefault(x => x.ID == model.ID).Slut = model.Slut;
                context.SaveChanges();
            }
            return View("Index");
        }
    }
}

模型

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; }
}
4

3 回答 3

0

这样的事情呢?

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

将字符串属性名称“Command”添加到您的模型中,并在您的操作中,如果做一件事,如果做另一件事 "model.Command" == "Start""model.Command" == "Create"例如

[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
    }
}
于 2013-11-15T01:45:41.740 回答
0

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

例如,您不能有嵌套表单:

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

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

</form>

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

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

  1. Javascript - 您可以使用 Javascript 以各种方式帮助您。action当您使用特定按钮提交时,它可以创建一个新的隐藏表单、创建隐藏字段或简单地更改表单。可能性很多。
  2. 重新思考——问问自己是否可以通过不同的方式实现相同的功能。
  3. 拆分表格- 你能把表格分成两个表格吗?请记住,表单只需要包含执行操作所需的数据(即输入字段),它们不需要包装大量不相关的 HTML。
于 2013-11-15T01:25:21.700 回答
0

一边开玩笑:

    @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");
}

注意:通常你会在中间添加一个额外的 DAL 层,比如存储库。我没有这样做以保持简洁。

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

于 2013-11-15T02:07:20.770 回答