2016-09-03 4 views
0

私は、ゴルフコースとそのそれぞれの額面のデータベーステーブルを持っています。私はテーブルに新しいゴルフコースの行を追加しようとしています。テーブルには、3列のID,CourseName、およびParがあります。C#/ MVC/Javascriptを使用してデータベースにレコードを追加します。

行を追加しようとすると、はIDフィールドなので、コードはCourseNameParの値を設定します。

New Courseボタンをクリックすると、コードは最初の行をテーブルから取得し、新しい行を作成する代わりにその行を編集します。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using GolfScore.Data; 

namespace GolfScore.Business 
{ 
public class CourseManager 
{ 
    public static void AddCourse(string Name, int Par) 
    { 
     using (DBContext context = new DBContext()) 
     { 
      Course j = context.Courses.Create(); 

      context.Courses.Add(j); 
      context.SaveChanges(); 

     } 
    } 

    public static void EditCourse(int? id, string Name, int Par) 
    { 
     using (DBContext context = new DBContext()) 
     { 

      Course t = context.Courses.Where(c => c.CourseId == id).FirstOrDefault(); 
      t.CourseName = Name; 
      t.Par = Par; 
      context.SaveChanges(); 
     } 
    } 

    public static Course GetCourse(int? id) 
    { 
     if (id.HasValue) { 
      using (DBContext context = new DBContext()) 
      { 
       return context.Courses.Where(c => c.CourseId == id.Value).FirstOrDefault(); 
      } 
     } 

     else 
     { 
      using (DBContext context = new DBContext()) 
      { 
       return context.Courses.FirstOrDefault(); 
      } 
     } 

    } 

    public static IList<Course> GetAllCourses() 
    { 
     using(DBContext context = new DBContext()) 
     { 
      return context.Courses.ToList(); 
     } 
    } 
} 

}

CourseController

using GolfScore.Business; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using GolfScore.Web.Models; 

namespace GolfScore.Web.Controllers 
{ 
    public class CourseController : Controller 
    { 
     // GET: Course 
     public ActionResult Index() 
     { 
      return View(CourseManager.GetAllCourses()); 
     } 

     [HttpGet] 
     public PartialViewResult AddEdit(int? id) 
     { 
      return PartialView("_AddEditCourse", CourseManager.GetCourse(id)); 
     } 

     [HttpPost] 
     public JsonResult Save(Course c) 
     { 
      if (c.Id.HasValue) 
      { 
       CourseManager.EditCourse(c.Id, c.Name, c.Par); 
      } 
      else 
      { 
       CourseManager.AddCourse(c.Name, c.Par); 
      } 
      return Json(new 
      { 
       Success = true 
      }); 
     } 
    } 
} 

Javascriptを

var Course = function (dialogSelector, addUrl, saveUrl) { 
    self = this; 
    self.dialogElement = dialogSelector; 
    self.addAJAXUrl = addUrl; 
    self.saveAJAXUrl = saveUrl; 
    self.dialog = null; 

    self.Initialize = function() { 
     self.dialog = $(self.dialogElement).dialog({ 
      autoOpen: false, 
      height: 400, 
      width: 350, 
      modal: true, 
      buttons: [ 
       { 
        text: "Save", 
        click: function() { 
         var jsonObject = { 
          "Id": parseInt($("#CourseId").val()), 
          "Name": $("#CourseName").val(), 
          "Par": parseInt($("#Par").val()) 
         }; 

         $.ajax({ 
          url: self.saveAJAXUrl, 
          type: "POST", 
          contentType: "application/json", 
          dataType: "json", 
          data: JSON.stringify(jsonObject), 
          success: function() { 
           self.dialog.dialog("close"); 
           location.reload(); 
          }, 
          error: function (a, b, c) { 
           alert("stupid"); 
          } 
         }) 
        } 
       }, 
       { 
        text: "Cancel", 
        click: function(){ 
         self.dialog.dialog("close"); 
        } 
       } 
      ], 
      close: function() { 
       self.dialog.dialog("close"); 
      } 
     }); 

     $(".newBtn").on("click", function() { 
      $.ajax({ 
       url: self.addAJAXUrl, 
       type: "GET", 
       success: function (data) { 
        $(self.dialogElement).html(data); 
        self.dialog.dialog("option", "title", "New Course"); 
        self.dialog.dialog("open"); 
       }, 
       error: function (a, b, c) { 
        alert("Error."); 
       } 
      }) 
     }); 

     $(".editBtn").on("click", function (e) { 
      $.ajax({ 
       url: self.addAJAXUrl + "?Id=" + $(e.target).attr("data-course-id"), 
       type: "GET", 
       success: function (data) { 
        $(self.dialogElement).html(data); 
        self.dialog.dialog("option", "title", "Edit Course"); 
        self.dialog.dialog("open"); 
       }, 
       error: function (a, b, c) { 
        alert("Error."); 
       } 
      }) 
     }); 
    }; 
} 

あなたが何かを必要とする場合、私に知らせてください。

ありがとうございます。

申し訳ございませんが、ここではコースのクラスです。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace GolfScore.Web.Models 
{ 
    [Serializable()] 
    public class Course 
    { 
     public int? Id { get; set; } 
     public string Name { get; set; } 
     public int Par { get; set; } 
    } 
} 
+0

あなたの 'Course'クラスのソースも表示したいかもしれません... –

+0

あなたの' GetCourse'メソッドは新しい要素の代わりに最初の要素を返さない –

+0

名前で実際に何もしていないあなたのコードに同行 – Eonasdan

答えて

0
 [HttpGet] 
     public PartialViewResult AddEdit(int? id) 
     { 
      return PartialView("_AddEditCourse", CourseManager.GetCourse(id)); 
     } 

あなたは、このように変更する必要があり、このアクション:

[HttpGet] 
public PartialViewResult AddEdit(int? id) 
{ 

    if(id.HasValue) 
     return PartialView("_AddEditCourse", CourseManager.GetCourse(id)); 

    return PartialView("_AddEditCourse",new Course()) 
} 

とidが値を持っている場合GetCourseのみコースを返さなければならない方法。

保存すると、新しいレコードがある場合、id = 0、otherwse id> 0になるので、id == 0であるかどうかを確認する必要があります。

編集:あなたは私はあなたがかみそりビューを掲示する必要がエラーを取得する理由は

public static Course GetCourse(int id){ 
using (DBContext context = new DBContext()) 
      { 
       return context.Courses.Find(id); 
      } 
} 

:コースを取得する

あなたの方法は次のようでなければなりません。

+0

助けてくれてありがとう!あなたが提案した変更を加えましたが、今度はAdd Courseボタンをクリックするとエラーが発生します。 GetCourseメソッドでは、コースIDがない場合はnullを返すように設定します。保存時にIDがない場合は、IDフィールドが自動生成されるため、コース名とparが返されます。少なくとも私は思う。私はこれにかなり新しいですし、私がそれを始める手助けをしてくれた人がいました。 –

+0

どのようなエラーが表示されますか?あなたの 'AddCourse'メソッドはOKではありません。空のコースを追加しています。 nameとparの値を新しいオブジェクトに与える必要があります。 –

+0

私は分かりません。 localhost:73293エラーというポップアップが表示されます。それ以上のことはありません。私は最終的にあなたがAddCourseメソッドで話していることを見て、それを修正しました。 –

関連する問題