###前提・実現したいこと
ASP.Net MVC5でWebシステムを作っています。
ユーザに必ず入力してもらいたい項目があるため、当該項目が未入力の状態ではデータを保存できないようにしたいです。
###発生している問題・エラーメッセージ
入力項目のうち、数項目は必須入力としたく、モデルにRequired属性を追加しました。(属性を定義していない項目もある。)
本来であれば、DB更新時に必須項目が未入力の場合はエラーメッセージが表示されると思うのですが、
エラーメッセージが何も表示されないまま、DB更新後に遷移させるIndex画面へ遷移してしまう。
一般的にこのような問題が発生する時に考えられる原因をご教示頂きたいです。
###試したこと
必須/非必須含め、全入力項目にデータを入力した状態にするとDB更新がされることは確認済み。
非必須項目を未入力とした場合も、DB更新がされない。Required属性を定義したのに原因不明。
###補足情報(言語/FW/ツール等のバージョンなど)
言語:C#
.NetFramework:4.5
ツール:Visual Studio2015 Community
すみません。問題を解決できないのでコードを追加しました。
例えば、項目"Price"は任意項目ですが、未入力状態でsaveしようとすると、何もエラーも表示されず、DB更新もされない状態です。
DB更新処理はEntity Framework4.5利用。
データベースはコードファーストで作成しました。
javascript
1View 2<script type="text/javascript"> 3function Quotation_save() { 4 // Step 1: Read View Data and Create JSON Object 5 6 // Creating SalesSub Json Object 7 var quotationsub = { 8 "QuotationId": "", "Line": "", "Grade": "", "Commodity": "", "Size": "", "metrictons": "", 9 "PC": "", "Price_detail": "", "Ideaprice_Offer_Bid_detail": "", "FOB_CFR_CIF_CIP_detail": "" 10 }; 11 12 13 // Creating SalesMain Json Object 14 var quotationmain = { 15 "QuotationId": "", "Customer": "", "Tender_Nontender": "", "SPOT_REPEAT": "", "TotalVolume": "", 16 "Price": "", "Ideaprice_Offer_Bid": "", "FOB_CFR_CIF_CIP": "", "PaymentTerm": "", "ShippingSchedule": "", 17 "Usage": "", "LoadingPort": "", "DischargingPort": "", "Country": "", "PreferredSupplier": "", "Remarks": "", "MailRecipient_To": "", "MailRecipient_Cc": "", "MailSender": "", "RegisterDate": "", "QuotationSubs": [] 18 }; 19 20 // Set Sales Main Value 21 quotationmain.QuotationId = $("#QuotationId").val(); 22 quotationmain.Customer = $("#Customer").val(); 23 quotationmain.Tender_Nontender = $("#Tender_Nontender").val(); 24 quotationmain.SPOT_REPEAT = $("#SPOT_REPEAT").val(); 25 quotationmain.TotalVolume = $("#TotalVolume").val(); 26 quotationmain.Price = $("#Price").val(); 27 quotationmain.Ideaprice_Offer_Bid = $("#Ideaprice_Offer_Bid").val(); 28 quotationmain.FOB_CFR_CIF_CIP = $("#FOB_CFR_CIF_CIP").val(); 29 quotationmain.PaymentTerm = $("#PaymentTerm").val(); 30 quotationmain.ShippingSchedule = $("#ShippingSchedule").val(); 31 quotationmain.Usage = $("#Usage").val(); 32 quotationmain.LoadingPort = $("#LoadingPort").val(); 33 quotationmain.DischargingPort = $("#DischargingPort").val(); 34 quotationmain.Country = $("#Country").val(); 35 quotationmain.PreferredSupplier = $("#PreferredSupplier").val(); 36 quotationmain.Remarks = $("#Remarks").val(); 37 quotationmain.MailRecipient_To = $("#MailRecipient_To").val(); 38 quotationmain.MailRecipient_Cc = $("#MailRecipient_Cc").val(); 39 quotationmain.MailSender = $("#MailSender").val(); 40 quotationmain.RegisterDate = $("#RegisterDate").val(); 41 42 43 // Getting Table Data from where we will fetch Sales Sub Record 44 var oTable = $('.tbl').dataTable().fnGetData(); 45 46 47 48 for (var i = 0; i < oTable.length; i++) 49 { 50 51 // IF This view is for edit then it will read SalesId from Hidden field 52 if ($('h2').text() == "Edit") 53 { 54 quotationsub.QuotationId = $('#QuotationId').val(); 55 } 56 else 57 { 58 quotationsub.QuotationId = 0; 59 } 60 } 61 // Step 1: Ends Here 62 63 64 // Set 2: Ajax Post 65 // Here i have used ajax post for saving/updating information 66 $.ajax({ 67 url: '/QuotationMains/Create', 68 data: JSON.stringify(quotationmain), 69 type: 'POST', 70 contentType: 'application/json;', 71 dataType: 'json', 72 success: function (result) { 73 74 if (result.Success == "1") { 75 window.location.href = "/QuotationMains/index"; 76 } 77 else { 78 alert(result.ex); 79 } 80 } 81 }); 82 83 84 } 85 86</script> 87@using (Html.BeginForm()) 88{ 89 @Html.ValidationSummary(true) 90 <fieldset> 91 <legend>Header</legend> 92 93 @if (Model != null) 94 { 95 96 <input type="hidden" id="QuotationId" name="SalesId" value="@Model.QuotationId" /> 97 } 98 99 <div class="form-horizontal"> 100 <hr /> 101 @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 102 <div class="form-group"> 103 @Html.LabelFor(model => model.Customer, htmlAttributes: new { @class = "control-label col-md-2" }) 104 <div class="col-md-10"> 105 @Html.EditorFor(model => model.Customer, new { htmlAttributes = new { @class = "form-control" } }) 106 @Html.ValidationMessageFor(model => model.Customer, "", new { @class = "text-danger" }) 107 </div> 108 </div> 109 <div class="form-group"> 110 @Html.LabelFor(model => model.TotalVolume, htmlAttributes: new { @class = "control-label col-md-2" }) 111 <div class="col-md-10"> 112 @Html.EditorFor(model => model.TotalVolume, new { htmlAttributes = new { @class = "form-control" } }) 113 @Html.ValidationMessageFor(model => model.TotalVolume, "", new { @class = "text-danger" }) 114 </div> 115 </div> 116 117 <div class="form-group"> 118 @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) 119 <div class="col-md-10"> 120 @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) 121 @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) 122 </div> 123 </div> 124 </div> 125 </fieldset> 126<input type="button" value="Save" onclick="Quotation_save()" /> 127}
C#
1controller 2[HttpPost] 3 public JsonResult Create(QuotationMain quotationmain) 4 { 5 try 6 { 7 if (ModelState.IsValid) 8 { 9 if (quotationmain.QuotationId > 0) 10 { 11 var CurrentquotationSUb = db.QuotationSubs.Where(p => p.QuotationId == quotationmain.QuotationId); 12 13 foreach (QuotationSub ss in CurrentquotationSUb) 14 db.QuotationSubs.Remove(ss); 15 16 foreach (QuotationSub ss in quotationmain.QuotationSubs) 17 db.QuotationSubs.Add(ss); 18 19 db.Entry(quotationmain).State = EntityState.Modified; 20 } 21 //Perform Save 22 else 23 { 24 db.QuotatinoMains.Add(quotationmain); 25 } 26 db.SaveChanges(); 27 return Json(new { Success = 1, QuotationId = quotationmain.QuotationId, ex = "" }); 28 } 29 else 30 { 31 return Json(new { Success = 1, ex = "ModelState is Invalid" }); 32 } 33 } 34 catch (Exception ex) 35 { 36 return Json(new { Success = 0, ex = ex.ToString() }); 37 } 38 39 }
C#
1model 2public class QuotationMain 3 { 4 [Key] 5 public int QuotationId { get; set; } 6 7 [Required] 8 [Display(Name = "Customer", ResourceType = typeof(Quotation3.ModelResource))] 9 public string Customer { get; set; } 10 11 [Display(Name = "TotalVolume", ResourceType = typeof(Quotation3.ModelResource))] 12 public int? TotalVolume { get; set; } 13 14 [Display(Name = "Price", ResourceType = typeof(Quotation3.ModelResource))] 15 public int? Price { get; set; } 16 17 [Required] 18 [EmailAddress] 19 [Display(Name = "MailRecipient_To", ResourceType = typeof(Quotation3.ModelResource))] 20 public string MailRecipient_To { get; set; } 21}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/17 08:25
2016/08/18 00:23
2016/08/18 03:18
2016/08/18 07:41
2016/08/18 16:55