前提・実現したいこと
ASP.NET MVCでWeb勤務表を作成しています。
DBから読み込んだデータ(日時、出勤時間等)をリスト表示するところまでは出来ましたが、そのデータをエディットで変更してDBを書き換えるところがわかりません(PostでViewからリストを受け取れない)。
詳細はソースコードに記します。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
MODEL:
namespace MvcShopping.Models { /// <summary> /// 勤務表情報のモデルクラス /// </summary> public class KintaiModel { //勤務表リスト public IQueryable<TKintai> Kintais { get; set; } } }
CONTROLLER:
namespace MvcShopping.Controllers { public class KintaiController : Controller { // // GET: /Kintai/ public ActionResult Index() { // web.config から接続文字列を取得 string cnstr = ConfigurationManager.ConnectionStrings[ "mvcdbConnectionString"].ConnectionString; // データベースに接続する DataContext dc = new DataContext(cnstr); // 商品一覧を取得 var list = from p in dc.GetTable<TKintai>() select p; return View(list); } // // POST: /Admin/Edit/5 [HttpPost] public ActionResult Index(KintaiModel kintai) { try { // web.config から接続文字列を取得 string cnstr = ConfigurationManager.ConnectionStrings[ "mvcdbConnectionString"].ConnectionString; // データベースに接続する DataContext dc = new DataContext(cnstr); var list = from p in dc.GetTable<TKintai>() select p; return RedirectToAction("Index"); } catch { return View(); } } } }
View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcShopping.Models.TKintai>>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <form id="form1" runat="server"> <h2> Index</h2> <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <table> <tr> <th> id </th> <th> week </th> <th> open </th> <th> close </th> <th> rest </th> <th> worktime </th> <th> overtime </th> <th> situation </th> <th> remark </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.id, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.id) %> </div> <%--<div class="display-field"> <%: item.id %></div>--%> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.week, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.week) %> </div> <%-- <div class="display-field"> <%: item.week %></div>--%> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.open, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.open) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.close, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.close) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.rest, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.rest) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.worktime, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.worktime) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.overtime, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.overtime) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.situation, new { style = "width:60px" })%> <%: Html.ValidationMessageFor(model => item.situation) %> </div> </td> <td> <div class="editor-field"> <%: Html.TextBoxFor(model => item.remark, new { style = "width:80px" })%> <%: Html.ValidationMessageFor(model => item.remark) %> </div> </td> </tr> <% } %> </table> <p> <input type="submit" value="更新" /> </p> </fieldset> <% } %> </form> </asp:Content>
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
※VisualStudio2010,C#
回答1件
あなたの回答
tips
プレビュー