2017-01-26 12 views
-1

データベースに入力された選択された値を持つようにcreate関数を取得しようとしています。作成ボタンを押すと、エラーはスローされませんが、データは移入されません。私は周波数フィールドが問題を引き起こしているとは確信していますが、解決策が出てこないのは確かです。データベースにデータを保存しない関数を作成します。

「通知名」の選択に応じて、ユーザーが選択できる頻度には2種類あります。 1つの選択には、数値、時間枠(週、月など)、前/後選択の3つの個別のフィールドがあります。もう1つは、瞬間的なものを静的なテキストフィールドとして表現します。どちらのオプションを選択しても、頻度データはデータベース内の1つのセルに取り込まれ、必要に応じて配管を使用して分離されます。私はまだC#MVCにはかなり新しいので、どんな助けも大歓迎です。

コントローラ:周波数オプションの

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 

     <hr /> 

     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 

      @Html.LabelFor(model => model.notificationType1, "Notification Name", htmlAttributes: new { @class = "control-label col-md-2 helper-format" }) 
      <div class="col-md-10" id="type_selection"> 
       @Html.DropDownList("notificationType1", new List<SelectListItem> { 
      new SelectListItem { Text = "Make a Selection", Value="" }, 
      new SelectListItem { Text = "Incomplete Documents", Value= "Incomplete Documents" }, 
      new SelectListItem { Text = "All Documents Complete", Value = "All Documents Complete" }, 
      new SelectListItem { Text = "Documents Requiring Action", Value = "Documents Requiring Action" } 
        }, new { @class = "helper-format", @id = "value_select", style = "font-family: 'Roboto', Sans Serif;" }) 
       @Html.ValidationMessageFor(model => model.notificationType1, "", new { @class = "text-danger" }) 
      </div> 

     </div> 
     <div class="form-group" id="frequency_group"> 
      @Html.LabelFor(model => model.frequency, "Frequency", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-sm-3" id="frequency_group"> 
       @Html.TextBoxFor(model => model.frequency, new { @class = "textbox-width", @placeholder = "42" }) 

       @Html.DropDownList("frequency", new List<SelectListItem> 
     { 
      new SelectListItem { Text = "Day(s)", Value= "| Day"}, 
      new SelectListItem { Text = "Week(s)", Value= "| Week"}, 
      new SelectListItem { Text = "Month(s)", Value= "| Month"} 
     }) 

       @Html.DropDownList("frequency", new List<SelectListItem> 
     { 
      new SelectListItem { Text = "Before", Value= "| Before"}, 
      new SelectListItem { Text = "After", Value= "| After"} 
     }) 
      </div> 
      <p class="col-sm-2" id="psdatetext">The Beginning</p> 
     </div> 
     <div class="form-group" id="freq_instant"> 
      @Html.LabelFor(model => model.frequency, "Frequency", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="instant_text"> 
      <p>Instantaneous</p></div> 

     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.recipientTypeId, "Notification For", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("recipientTypeId", new List<SelectListItem> 
     { 
      new SelectListItem { Text = "Me", Value= "Me"}, 
      new SelectListItem { Text = "Account Manager", Value="Account Manager" }, 
      new SelectListItem { Text = "Candidate", Value= "Candidate"}, 
      new SelectListItem { Text = "Recruiter", Value="Recruiter" }, 
      new SelectListItem { Text = "Manager", Value= "Manager"} 
     }) 
      </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-1 col-md-10"> 
       <div id="hovercreate"> 
        <button type="submit" value="CREATE" class="btn btn-primary" id="createbtn">CREATE</button> 
       </div> 

      </div> 

     </div> 
    </div> 
} 

JS

@Scripts.Render("~/bundles/jquery") 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('#frequency_group').hide() 
    $('#freq_instant').hide() 

    $('#value_select').change(function() { 
     var selection = $('#value_select').val(); 
     $('#frequency_group').hide(); 
     switch (selection) { 
      case 'Incomplete Documents': 
       $('#frequency_group').show(); 
       break; 
      case 'All Documents Complete': 
       $('#frequency_group').show(); 
       break; 
     } 
    }); 

    $('#value_select').on('change', function() { 
     if (this.value == 'Documents Requiring Action') { 
      $("#freq_instant").show(); 
     } 
     else { 
      $("#freq_instant").hide(); 
     } 
    }); 

}); 

+0

この使用してEntity Frameworkのコードファーストですか?もしそうなら、発行されているSQLをチェックするのはなぜですか? ctx.Database.Logにロガーを追加して、トレース出力に書き込んでください。 –

+0

ブラウザのネットワークツールを使用してHTTPリクエストを監視します。フォームは適切なURLにPOSTされていますか? – mason

+1

ブレークポイントを設定し、デバッグし、送信しているデータにエラーがあるかどうかを確認します。 – Dhanashree

答えて

1

メソッドにブレークポイントを設定しましたか?もしそうなら、それはトリガーですか?

ない場合は、これを試して...私が覚えているから

、すべてのコントローラがRouteConfig.csファイル(App_Start/RouteConfig.cs)に設定されているIDのデフォルトパラメータを持っています。

そこから行く方法はいくつかあります。あなたがする必要があるこれを行うには

属性 1.ルートを経由して、ルートの値を設定し、コントローラにIDパラメータ(例えば(int型のID)) 2.を与える - A.あなたRouteConfigの先頭に次の行を追加します。 .cs/RegisterRoutesメソッド。 B.は

[ValidateAntiForgeryToken] 
[Route(@"Create/")] 
public ActionResult Create([Bind(Include = ... 
{ 

を追加

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");     
    routes.MapMvcAttributeRoutes(); 
    //... 
} 

私もそのがそれを打つかどうかを確認する方法の最初にブレークポイントを置くことを示唆しています。

http://www.tutorialsteacher.com/mvc/routing-in-mvc https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionattributeroutingextensions.mapmvcattributeroutes%28v=vs.118%29.aspx

1

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "Id,notificationType1,recipientTypeId,frequency")] NotificationType notificationType) 
    { 
     if (ModelState.IsValid) 
     { 
      db.NotificationType.Add(notificationType); 
      db.SaveChanges(); 
      return RedirectToAction("Create"); 
     } 

     ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole", notificationType.recipientTypeId); 
     return View(notificationType); 
    } 

ビューが手動で割り当てられたIDキーですか?そうでない場合(IDENTITYフィールドの場合など)、バインドしてはいけません - [Bind(Include = "...")]からIDを削除してください。

関連する問題