こんにちわ。
現在ASP.NET MVC5(VisualStudio2017)で下図のようなWeb勤務表を作っています。
「定時」ボタンを押したときにある決められた時刻を始業時刻と終業時刻に挿入したいのですが、なかなかできなくて困ってます。たとえば定時ボタンを押すと開始時刻に9:00、終業時刻に18:00とテキストボックスに入るようにしたいです。
下記に画面のスクショとコードを載せておきます。よろしくお願いします。
ビュー:
@model IList<Kintai_CS_.Models.TKintai> @{ /**/ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; Dictionary<int, SelectList> dictionary = (Dictionary<int, SelectList>)ViewBag.Situation; } <h2>11月</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <table class="table"> <tr> <th> <nobr>日付</nobr> </th> <th> <nobr>曜日</nobr> </th> <th> <nobr>始業時刻</nobr> </th> <th> <nobr>終業時刻</nobr> </th> <th> <nobr>休憩時間</nobr> </th> <th> <nobr>所定時間</nobr> </th> <th> <nobr>残業時間</nobr> </th> <th> <nobr>勤怠状況</nobr> </th> <th> <nobr>補助</nobr> </th> <th> <nobr>備考</nobr> </th> </tr> @for (int i = 0; i < Model.Count; i++) { int idx = i; <tr> <td> @Html.DisplayFor(model => model[idx].id) @Html.HiddenFor(model => model[idx].id) </td> <td> @Html.DisplayFor(model => model[idx].week) @Html.HiddenFor(model => model[idx].week) </td> <td> @Html.EditorFor(model => model[idx].open, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model[idx].open, "", new { @class = "text-danger" }) </td> <td> @Html.EditorFor(model => model[idx].close, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model[idx].close, "", new { @class = "text-danger" }) </td> <td> @Html.EditorFor(model => model[idx].rest, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model[idx].rest, "", new { @class = "text-danger" }) </td> <td> @Html.DisplayFor(model => model[idx].worktime) @Html.HiddenFor(model => model[idx].worktime) </td> <td> @Html.DisplayFor(model => model[idx].overtime) @Html.HiddenFor(model => model[idx].overtime) </td> <td> @Html.DropDownListFor(model => model[idx].situation, dictionary[Model[idx].situation], new { htmlAttributes = new { @class = "form-control" } }) </td> <td style="width: 66px"> <nobr> <button type="button" onclick="location.href='@Url.Action("BtnClick","Kintai")'">定時</button> </nobr> </td> <td> @Html.EditorFor(model => model[idx].remark, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model[idx].remark, "", new { @class = "text-danger" }) </td> </tr> } </table> <p> <input type="submit" value="更新" /> </p> }
コントローラ:
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using Kintai_CS_.Models; namespace Kintai_CS_.Controllers { public class TKintaisController : Controller { private mvcdbEntities db = new mvcdbEntities(); // GET: TKintais public ActionResult Index() { //選択ボックスのオプションを準備 List<TSituation> situations = db.TSituation.ToList(); Dictionary<int, SelectList> dictionary = new Dictionary<int, SelectList>(); foreach (TSituation item in situations) { dictionary.Add(item.id, new SelectList(situations, "ID", "Situation", item.id)); } ViewBag.Situation = dictionary; return View(db.TKintai.ToList()); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Index(List<TKintai> products) { foreach(var kintai in products) { TKintai sv_product = db.TKintai.Find(kintai.id); sv_product.open = kintai.open; sv_product.close = kintai.close; sv_product.rest = kintai.rest; sv_product.situation = kintai.situation; sv_product.remark = kintai.remark; if (kintai.open != null && kintai.close != null && kintai.rest != null) { kintai.worktime = kintai.close - kintai.open - kintai.rest; kintai.overtime = kintai.worktime - TimeSpan.FromHours(8); } } db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/07 00:36
退会済みユーザー
2018/12/07 01:24 編集
2018/12/07 05:59 編集
退会済みユーザー
2018/12/07 07:33
2018/12/07 08:55
退会済みユーザー
2018/12/08 05:50
退会済みユーザー
2018/12/11 08:46
2018/12/11 11:36
2018/12/12 05:10
2018/12/25 08:19
退会済みユーザー
2018/12/25 09:31
2018/12/25 23:57