質問編集履歴

1

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

2016/08/17 08:22

投稿

_Mar85
_Mar85

スコア13

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,401 @@
35
35
  .NetFramework:4.5
36
36
 
37
37
  ツール:Visual Studio2015 Community
38
+
39
+
40
+
41
+ すみません。問題を解決できないのでコードを追加しました。
42
+
43
+ 例えば、項目"Price"は任意項目ですが、未入力状態でsaveしようとすると、何もエラーも表示されず、DB更新もされない状態です。
44
+
45
+ DB更新処理はEntity Framework4.5利用。
46
+
47
+ データベースはコードファーストで作成しました。
48
+
49
+
50
+
51
+ ```javascript
52
+
53
+ View
54
+
55
+ <script type="text/javascript">
56
+
57
+ function Quotation_save() {
58
+
59
+ // Step 1: Read View Data and Create JSON Object
60
+
61
+
62
+
63
+ // Creating SalesSub Json Object
64
+
65
+ var quotationsub = {
66
+
67
+ "QuotationId": "", "Line": "", "Grade": "", "Commodity": "", "Size": "", "metrictons": "",
68
+
69
+ "PC": "", "Price_detail": "", "Ideaprice_Offer_Bid_detail": "", "FOB_CFR_CIF_CIP_detail": ""
70
+
71
+ };
72
+
73
+
74
+
75
+
76
+
77
+ // Creating SalesMain Json Object
78
+
79
+ var quotationmain = {
80
+
81
+ "QuotationId": "", "Customer": "", "Tender_Nontender": "", "SPOT_REPEAT": "", "TotalVolume": "",
82
+
83
+ "Price": "", "Ideaprice_Offer_Bid": "", "FOB_CFR_CIF_CIP": "", "PaymentTerm": "", "ShippingSchedule": "",
84
+
85
+ "Usage": "", "LoadingPort": "", "DischargingPort": "", "Country": "", "PreferredSupplier": "", "Remarks": "", "MailRecipient_To": "", "MailRecipient_Cc": "", "MailSender": "", "RegisterDate": "", "QuotationSubs": []
86
+
87
+ };
88
+
89
+
90
+
91
+ // Set Sales Main Value
92
+
93
+ quotationmain.QuotationId = $("#QuotationId").val();
94
+
95
+ quotationmain.Customer = $("#Customer").val();
96
+
97
+ quotationmain.Tender_Nontender = $("#Tender_Nontender").val();
98
+
99
+ quotationmain.SPOT_REPEAT = $("#SPOT_REPEAT").val();
100
+
101
+ quotationmain.TotalVolume = $("#TotalVolume").val();
102
+
103
+ quotationmain.Price = $("#Price").val();
104
+
105
+ quotationmain.Ideaprice_Offer_Bid = $("#Ideaprice_Offer_Bid").val();
106
+
107
+ quotationmain.FOB_CFR_CIF_CIP = $("#FOB_CFR_CIF_CIP").val();
108
+
109
+ quotationmain.PaymentTerm = $("#PaymentTerm").val();
110
+
111
+ quotationmain.ShippingSchedule = $("#ShippingSchedule").val();
112
+
113
+ quotationmain.Usage = $("#Usage").val();
114
+
115
+ quotationmain.LoadingPort = $("#LoadingPort").val();
116
+
117
+ quotationmain.DischargingPort = $("#DischargingPort").val();
118
+
119
+ quotationmain.Country = $("#Country").val();
120
+
121
+ quotationmain.PreferredSupplier = $("#PreferredSupplier").val();
122
+
123
+ quotationmain.Remarks = $("#Remarks").val();
124
+
125
+ quotationmain.MailRecipient_To = $("#MailRecipient_To").val();
126
+
127
+ quotationmain.MailRecipient_Cc = $("#MailRecipient_Cc").val();
128
+
129
+ quotationmain.MailSender = $("#MailSender").val();
130
+
131
+ quotationmain.RegisterDate = $("#RegisterDate").val();
132
+
133
+
134
+
135
+
136
+
137
+ // Getting Table Data from where we will fetch Sales Sub Record
138
+
139
+ var oTable = $('.tbl').dataTable().fnGetData();
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+ for (var i = 0; i < oTable.length; i++)
148
+
149
+ {
150
+
151
+
152
+
153
+ // IF This view is for edit then it will read SalesId from Hidden field
154
+
155
+ if ($('h2').text() == "Edit")
156
+
157
+ {
158
+
159
+ quotationsub.QuotationId = $('#QuotationId').val();
160
+
161
+ }
162
+
163
+ else
164
+
165
+ {
166
+
167
+ quotationsub.QuotationId = 0;
168
+
169
+ }
170
+
171
+ }
172
+
173
+ // Step 1: Ends Here
174
+
175
+
176
+
177
+
178
+
179
+ // Set 2: Ajax Post
180
+
181
+ // Here i have used ajax post for saving/updating information
182
+
183
+ $.ajax({
184
+
185
+ url: '/QuotationMains/Create',
186
+
187
+ data: JSON.stringify(quotationmain),
188
+
189
+ type: 'POST',
190
+
191
+ contentType: 'application/json;',
192
+
193
+ dataType: 'json',
194
+
195
+ success: function (result) {
196
+
197
+
198
+
199
+ if (result.Success == "1") {
200
+
201
+ window.location.href = "/QuotationMains/index";
202
+
203
+ }
204
+
205
+ else {
206
+
207
+ alert(result.ex);
208
+
209
+ }
210
+
211
+ }
212
+
213
+ });
214
+
215
+
216
+
217
+
218
+
219
+ }
220
+
221
+
222
+
223
+ </script>
224
+
225
+ @using (Html.BeginForm())
226
+
227
+ {
228
+
229
+ @Html.ValidationSummary(true)
230
+
231
+ <fieldset>
232
+
233
+ <legend>Header</legend>
234
+
235
+
236
+
237
+ @if (Model != null)
238
+
239
+ {
240
+
241
+
242
+
243
+ <input type="hidden" id="QuotationId" name="SalesId" value="@Model.QuotationId" />
244
+
245
+ }
246
+
247
+
248
+
249
+ <div class="form-horizontal">
250
+
251
+ <hr />
252
+
253
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" })
254
+
255
+ <div class="form-group">
256
+
257
+ @Html.LabelFor(model => model.Customer, htmlAttributes: new { @class = "control-label col-md-2" })
258
+
259
+ <div class="col-md-10">
260
+
261
+ @Html.EditorFor(model => model.Customer, new { htmlAttributes = new { @class = "form-control" } })
262
+
263
+ @Html.ValidationMessageFor(model => model.Customer, "", new { @class = "text-danger" })
264
+
265
+ </div>
266
+
267
+ </div>
268
+
269
+ <div class="form-group">
270
+
271
+ @Html.LabelFor(model => model.TotalVolume, htmlAttributes: new { @class = "control-label col-md-2" })
272
+
273
+ <div class="col-md-10">
274
+
275
+ @Html.EditorFor(model => model.TotalVolume, new { htmlAttributes = new { @class = "form-control" } })
276
+
277
+ @Html.ValidationMessageFor(model => model.TotalVolume, "", new { @class = "text-danger" })
278
+
279
+ </div>
280
+
281
+ </div>
282
+
283
+
284
+
285
+ <div class="form-group">
286
+
287
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
288
+
289
+ <div class="col-md-10">
290
+
291
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
292
+
293
+ @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
294
+
295
+ </div>
296
+
297
+ </div>
298
+
299
+ </div>
300
+
301
+ </fieldset>
302
+
303
+ <input type="button" value="Save" onclick="Quotation_save()" />
304
+
305
+ }
306
+
307
+ ```
308
+
309
+ ```C#
310
+
311
+ controller
312
+
313
+ [HttpPost]
314
+
315
+ public JsonResult Create(QuotationMain quotationmain)
316
+
317
+ {
318
+
319
+ try
320
+
321
+ {
322
+
323
+ if (ModelState.IsValid)
324
+
325
+ {
326
+
327
+ if (quotationmain.QuotationId > 0)
328
+
329
+ {
330
+
331
+ var CurrentquotationSUb = db.QuotationSubs.Where(p => p.QuotationId == quotationmain.QuotationId);
332
+
333
+
334
+
335
+ foreach (QuotationSub ss in CurrentquotationSUb)
336
+
337
+ db.QuotationSubs.Remove(ss);
338
+
339
+
340
+
341
+ foreach (QuotationSub ss in quotationmain.QuotationSubs)
342
+
343
+ db.QuotationSubs.Add(ss);
344
+
345
+
346
+
347
+ db.Entry(quotationmain).State = EntityState.Modified;
348
+
349
+ }
350
+
351
+ //Perform Save
352
+
353
+ else
354
+
355
+ {
356
+
357
+ db.QuotatinoMains.Add(quotationmain);
358
+
359
+ }
360
+
361
+ db.SaveChanges();
362
+
363
+ return Json(new { Success = 1, QuotationId = quotationmain.QuotationId, ex = "" });
364
+
365
+ }
366
+
367
+ else
368
+
369
+ {
370
+
371
+ return Json(new { Success = 1, ex = "ModelState is Invalid" });
372
+
373
+ }
374
+
375
+ }
376
+
377
+ catch (Exception ex)
378
+
379
+ {
380
+
381
+ return Json(new { Success = 0, ex = ex.ToString() });
382
+
383
+ }
384
+
385
+
386
+
387
+ }
388
+
389
+ ```
390
+
391
+ ```C#
392
+
393
+ model
394
+
395
+ public class QuotationMain
396
+
397
+ {
398
+
399
+ [Key]
400
+
401
+ public int QuotationId { get; set; }
402
+
403
+
404
+
405
+ [Required]
406
+
407
+ [Display(Name = "Customer", ResourceType = typeof(Quotation3.ModelResource))]
408
+
409
+ public string Customer { get; set; }
410
+
411
+
412
+
413
+ [Display(Name = "TotalVolume", ResourceType = typeof(Quotation3.ModelResource))]
414
+
415
+ public int? TotalVolume { get; set; }
416
+
417
+
418
+
419
+ [Display(Name = "Price", ResourceType = typeof(Quotation3.ModelResource))]
420
+
421
+ public int? Price { get; set; }
422
+
423
+
424
+
425
+ [Required]
426
+
427
+ [EmailAddress]
428
+
429
+ [Display(Name = "MailRecipient_To", ResourceType = typeof(Quotation3.ModelResource))]
430
+
431
+ public string MailRecipient_To { get; set; }
432
+
433
+ }
434
+
435
+ ```