こんにちわ。
ASP.NET MVC5でWeb勤務表を作成していますが、ビューで編集したデータをリストでコントロールにポストした後の処理で困っています。
Postで受け取ったリストでデータベースを更新したいのですが、どのようにコーディングしていいか分かりません。
以前Entitityで使ったときのコードをそのまま乗せてみましたが、「'TKintai' は 'System.Data.Linq.DataContext' のメンバーではありません。」というエラーがでます。<HttpPost()>のDim sv_product = dc.TKintai.Find(product.id)のところでエラーがでます。
Dim sv_product = dc.TKintai.Find(product.id)を書き換えればうまくいくと思いますが、どうしていいかわかりません。
以下にコードを記します。一応モデルとビューも載せます。
コントローラ:
Imports System.Configuration Imports System.Data.Linq Imports Kintai.HomeModels Imports System Imports System.Collections.Generic Imports System.Data Imports System.Linq Imports System.Net Imports System.Web Imports System.Web.Mvc Imports Kintai Namespace Kintai Public Class KintaiController Inherits System.Web.Mvc.Controller Dim cnstr As String = ConfigurationManager.ConnectionStrings( "mvcdbConnectionString").ConnectionString Dim dc As DataContext = New DataContext(cnstr) ' ' GET: /Kintai Function Index() As ActionResult Dim list = From p In dc.GetTable(Of TKintai)() Select New HomeModels.Kintai With { .id = p.id, .week = p.week, .open = p.open, .close = p.close, .rest = p.rest, .situation = p.situation, .remark = p.remark, .worktime = p.worktime, .overtime = p.overtime } Return View(list.ToList()) End Function <HttpPost()> Function Index(products As List(Of TKintai)) As ActionResult For Each product In products Dim sv_product = dc.TKintai.Find(product.id) sv_product.open = product.open sv_product.close = product.close sv_product.rest = product.rest sv_product.situation = product.situation sv_product.remark = product.remark If Not product.open Is Nothing And Not product.close Is Nothing And Not product.rest Is Nothing Then sv_product.worktime = product.close - product.open - product.rest sv_product.overtime = sv_product.worktime - TimeSpan.FromHours(8) End If Next dc.SubmitChanges() Return RedirectToAction("Index") End Function End Class End Namespace
モデル:
Imports System.ComponentModel.DataAnnotations Imports System.ComponentModel Public Class HomeModels Public Class Kintai <Required()> <DisplayName("日付")> Public Property id As Integer <Required()> <DisplayName("曜日")> Public Property week As String <Required()> <DisplayName("開始")> Public Property open As Nullable(Of System.TimeSpan) <Required()> <DisplayName("終了")> Public Property close As Nullable(Of System.TimeSpan) <Required()> <DisplayName("休憩")> Public Property rest As Nullable(Of System.TimeSpan) <Required()> <DisplayName("状況")> Public Property situation As String <Required()> <DisplayName("備考")> Public Property remark As String <Required()> <DisplayName("所定時間")> Public Property worktime As Nullable(Of System.TimeSpan) <Required()> <DisplayName("残業時間")> Public Property overtime As Nullable(Of System.TimeSpan) End Class End Class
ビュー:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of List (Of Kintai.HomeModels+Kintai))" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Index</h2> <% Using (Html.BeginForm()) Html.AntiForgeryToken()%> <p> <%: Html.ActionLink("Create New", "Create")%> </p> <table> <tr> <th> id </th> <th> week </th> <th> open </th> <th> close </th> <th> rest </th> <th> situation </th> <th> remark </th> <th> worktime </th> <th> overtime </th> </tr> <% For i = 0 To Model.Count - 1 Dim idx As Integer = i %> <tr> <td> <%: Html.DisplayFor(Function(model) model(idx).id)%> <%: Html.HiddenFor(Function(model) model(idx).id)%> </td> <td> <%: Html.DisplayFor(Function(model) model(idx).week)%> <%: Html.HiddenFor(Function(model) model(idx).week)%> </td> <td> <%:Html.EditorFor(Function(model) model(idx).open, New With {.htmlAttributes = New With {.class = "form-control"}})%> <%:Html.ValidationMessageFor(Function(model) model(idx).open, "", New With {.class = "text-danger"}) %> </td> <td> <%:Html.EditorFor(Function(model) model(idx).close, New With {.htmlAttributes = New With {.class = "form-control"}})%> <%:Html.ValidationMessageFor(Function(model) model(idx).close, "", New With {.class = "text-danger"})%> </td> <td> <%:Html.EditorFor(Function(model) model(idx).rest, New With {.htmlAttributes = New With {.class = "form-control"}})%> <%:Html.ValidationMessageFor(Function(model) model(idx).rest, "", New With {.class = "text-danger"})%> </td> <td> <%: Html.DisplayFor(Function(model) model(idx).worktime)%> <%: Html.HiddenFor(Function(model) model(idx).worktime)%> </td> <td> <%: Html.DisplayFor(Function(model) model(idx).overtime)%> <%: Html.HiddenFor(Function(model) model(idx).overtime)%> </td> <td> <nobr> <%:Html.ActionLink("定", "Edit", New With {.id = Model(idx).id})%> <%:Html.ActionLink("前", "Edit", New With {.id = Model(idx).id})%> </nobr> </td> <td> <%:Html.EditorFor(Function(model) model(idx).remark, New With {.htmlAttributes = New With {.class = "form-control"}})%> <%:Html.ValidationMessageFor(Function(model) model(idx).remark, "", New With {.class = "text-danger"})%> </td> </tr> <% Next%> </table> <div Class="form-group"> <div Class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" Class="btn btn-default" /> </div> </div> <% End Using%> </asp:Content>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。