質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
VB

VB(ビジュアルベーシック)はマイクロソフトによってつくられたオブジェクト指向プログラミング言語のひとつで、同社のQuickBASICが拡張されたものです。VB6の進化版といわれています。

ASP.NET MVC Framework

ASP.NET MVC Frameworkは、MVCパターンをベースとした、マイクロソフトのウェブアプリケーション開発用のフレームワークです。

Q&A

解決済

1回答

2061閲覧

ASP.NET MVC5でLINQ TO SQLクラスのリストデータを更新する方法

blackdifferent

総合スコア25

VB

VB(ビジュアルベーシック)はマイクロソフトによってつくられたオブジェクト指向プログラミング言語のひとつで、同社のQuickBASICが拡張されたものです。VB6の進化版といわれています。

ASP.NET MVC Framework

ASP.NET MVC Frameworkは、MVCパターンをベースとした、マイクロソフトのウェブアプリケーション開発用のフレームワークです。

0グッド

0クリップ

投稿2018/11/28 07:46

編集2019/01/08 01:48

こんにちわ。
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>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自分で調べていたらとりあえず求めていた機能が出来ました。

<HttpPost()> Function Index(products As List(Of TKintai)) As ActionResult Dim cnstr As String = ConfigurationManager.ConnectionStrings( "mvcdbConnectionString").ConnectionString Dim dc As DataContext = New DataContext(cnstr) Dim sv_product As TKintai For Each product In products Dim i As Integer = product.id sv_product = dc.GetTable(Of TKintai)().Single(Function(t) t.id = i) 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

投稿2018/11/30 03:44

blackdifferent

総合スコア25

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問