0

これがコードです。「開始」を押すと2つの送信ボタンがあり、Datetime.nowを開始行に送信し、「停止」を押すと停止datetime.nowを列、これは同じ行で発生する必要があります。そして、もう一度[開始]を押すと、新しいID 2などが生成され、2行目に開始日が出力されます。

例 ID 1 : 開始 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 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

冗談はさておき:

    @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 に答える
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 に答える