teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コード\(View/Controller/Model\)を追加。

2016/08/17 08:22

投稿

_Mar85
_Mar85

スコア13

title CHANGED
File without changes
body CHANGED
@@ -16,4 +16,203 @@
16
16
  ###補足情報(言語/FW/ツール等のバージョンなど)
17
17
  言語:C#
18
18
  .NetFramework:4.5
19
- ツール:Visual Studio2015 Community
19
+ ツール:Visual Studio2015 Community
20
+
21
+ すみません。問題を解決できないのでコードを追加しました。
22
+ 例えば、項目"Price"は任意項目ですが、未入力状態でsaveしようとすると、何もエラーも表示されず、DB更新もされない状態です。
23
+ DB更新処理はEntity Framework4.5利用。
24
+ データベースはコードファーストで作成しました。
25
+
26
+ ```javascript
27
+ View
28
+ <script type="text/javascript">
29
+ function Quotation_save() {
30
+ // Step 1: Read View Data and Create JSON Object
31
+
32
+ // Creating SalesSub Json Object
33
+ var quotationsub = {
34
+ "QuotationId": "", "Line": "", "Grade": "", "Commodity": "", "Size": "", "metrictons": "",
35
+ "PC": "", "Price_detail": "", "Ideaprice_Offer_Bid_detail": "", "FOB_CFR_CIF_CIP_detail": ""
36
+ };
37
+
38
+
39
+ // Creating SalesMain Json Object
40
+ var quotationmain = {
41
+ "QuotationId": "", "Customer": "", "Tender_Nontender": "", "SPOT_REPEAT": "", "TotalVolume": "",
42
+ "Price": "", "Ideaprice_Offer_Bid": "", "FOB_CFR_CIF_CIP": "", "PaymentTerm": "", "ShippingSchedule": "",
43
+ "Usage": "", "LoadingPort": "", "DischargingPort": "", "Country": "", "PreferredSupplier": "", "Remarks": "", "MailRecipient_To": "", "MailRecipient_Cc": "", "MailSender": "", "RegisterDate": "", "QuotationSubs": []
44
+ };
45
+
46
+ // Set Sales Main Value
47
+ quotationmain.QuotationId = $("#QuotationId").val();
48
+ quotationmain.Customer = $("#Customer").val();
49
+ quotationmain.Tender_Nontender = $("#Tender_Nontender").val();
50
+ quotationmain.SPOT_REPEAT = $("#SPOT_REPEAT").val();
51
+ quotationmain.TotalVolume = $("#TotalVolume").val();
52
+ quotationmain.Price = $("#Price").val();
53
+ quotationmain.Ideaprice_Offer_Bid = $("#Ideaprice_Offer_Bid").val();
54
+ quotationmain.FOB_CFR_CIF_CIP = $("#FOB_CFR_CIF_CIP").val();
55
+ quotationmain.PaymentTerm = $("#PaymentTerm").val();
56
+ quotationmain.ShippingSchedule = $("#ShippingSchedule").val();
57
+ quotationmain.Usage = $("#Usage").val();
58
+ quotationmain.LoadingPort = $("#LoadingPort").val();
59
+ quotationmain.DischargingPort = $("#DischargingPort").val();
60
+ quotationmain.Country = $("#Country").val();
61
+ quotationmain.PreferredSupplier = $("#PreferredSupplier").val();
62
+ quotationmain.Remarks = $("#Remarks").val();
63
+ quotationmain.MailRecipient_To = $("#MailRecipient_To").val();
64
+ quotationmain.MailRecipient_Cc = $("#MailRecipient_Cc").val();
65
+ quotationmain.MailSender = $("#MailSender").val();
66
+ quotationmain.RegisterDate = $("#RegisterDate").val();
67
+
68
+
69
+ // Getting Table Data from where we will fetch Sales Sub Record
70
+ var oTable = $('.tbl').dataTable().fnGetData();
71
+
72
+
73
+
74
+ for (var i = 0; i < oTable.length; i++)
75
+ {
76
+
77
+ // IF This view is for edit then it will read SalesId from Hidden field
78
+ if ($('h2').text() == "Edit")
79
+ {
80
+ quotationsub.QuotationId = $('#QuotationId').val();
81
+ }
82
+ else
83
+ {
84
+ quotationsub.QuotationId = 0;
85
+ }
86
+ }
87
+ // Step 1: Ends Here
88
+
89
+
90
+ // Set 2: Ajax Post
91
+ // Here i have used ajax post for saving/updating information
92
+ $.ajax({
93
+ url: '/QuotationMains/Create',
94
+ data: JSON.stringify(quotationmain),
95
+ type: 'POST',
96
+ contentType: 'application/json;',
97
+ dataType: 'json',
98
+ success: function (result) {
99
+
100
+ if (result.Success == "1") {
101
+ window.location.href = "/QuotationMains/index";
102
+ }
103
+ else {
104
+ alert(result.ex);
105
+ }
106
+ }
107
+ });
108
+
109
+
110
+ }
111
+
112
+ </script>
113
+ @using (Html.BeginForm())
114
+ {
115
+ @Html.ValidationSummary(true)
116
+ <fieldset>
117
+ <legend>Header</legend>
118
+
119
+ @if (Model != null)
120
+ {
121
+
122
+ <input type="hidden" id="QuotationId" name="SalesId" value="@Model.QuotationId" />
123
+ }
124
+
125
+ <div class="form-horizontal">
126
+ <hr />
127
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" })
128
+ <div class="form-group">
129
+ @Html.LabelFor(model => model.Customer, htmlAttributes: new { @class = "control-label col-md-2" })
130
+ <div class="col-md-10">
131
+ @Html.EditorFor(model => model.Customer, new { htmlAttributes = new { @class = "form-control" } })
132
+ @Html.ValidationMessageFor(model => model.Customer, "", new { @class = "text-danger" })
133
+ </div>
134
+ </div>
135
+ <div class="form-group">
136
+ @Html.LabelFor(model => model.TotalVolume, htmlAttributes: new { @class = "control-label col-md-2" })
137
+ <div class="col-md-10">
138
+ @Html.EditorFor(model => model.TotalVolume, new { htmlAttributes = new { @class = "form-control" } })
139
+ @Html.ValidationMessageFor(model => model.TotalVolume, "", new { @class = "text-danger" })
140
+ </div>
141
+ </div>
142
+
143
+ <div class="form-group">
144
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
145
+ <div class="col-md-10">
146
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
147
+ @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
148
+ </div>
149
+ </div>
150
+ </div>
151
+ </fieldset>
152
+ <input type="button" value="Save" onclick="Quotation_save()" />
153
+ }
154
+ ```
155
+ ```C#
156
+ controller
157
+ [HttpPost]
158
+ public JsonResult Create(QuotationMain quotationmain)
159
+ {
160
+ try
161
+ {
162
+ if (ModelState.IsValid)
163
+ {
164
+ if (quotationmain.QuotationId > 0)
165
+ {
166
+ var CurrentquotationSUb = db.QuotationSubs.Where(p => p.QuotationId == quotationmain.QuotationId);
167
+
168
+ foreach (QuotationSub ss in CurrentquotationSUb)
169
+ db.QuotationSubs.Remove(ss);
170
+
171
+ foreach (QuotationSub ss in quotationmain.QuotationSubs)
172
+ db.QuotationSubs.Add(ss);
173
+
174
+ db.Entry(quotationmain).State = EntityState.Modified;
175
+ }
176
+ //Perform Save
177
+ else
178
+ {
179
+ db.QuotatinoMains.Add(quotationmain);
180
+ }
181
+ db.SaveChanges();
182
+ return Json(new { Success = 1, QuotationId = quotationmain.QuotationId, ex = "" });
183
+ }
184
+ else
185
+ {
186
+ return Json(new { Success = 1, ex = "ModelState is Invalid" });
187
+ }
188
+ }
189
+ catch (Exception ex)
190
+ {
191
+ return Json(new { Success = 0, ex = ex.ToString() });
192
+ }
193
+
194
+ }
195
+ ```
196
+ ```C#
197
+ model
198
+ public class QuotationMain
199
+ {
200
+ [Key]
201
+ public int QuotationId { get; set; }
202
+
203
+ [Required]
204
+ [Display(Name = "Customer", ResourceType = typeof(Quotation3.ModelResource))]
205
+ public string Customer { get; set; }
206
+
207
+ [Display(Name = "TotalVolume", ResourceType = typeof(Quotation3.ModelResource))]
208
+ public int? TotalVolume { get; set; }
209
+
210
+ [Display(Name = "Price", ResourceType = typeof(Quotation3.ModelResource))]
211
+ public int? Price { get; set; }
212
+
213
+ [Required]
214
+ [EmailAddress]
215
+ [Display(Name = "MailRecipient_To", ResourceType = typeof(Quotation3.ModelResource))]
216
+ public string MailRecipient_To { get; set; }
217
+ }
218
+ ```