我有一个高尔夫球场的数据库表及其各自的面值。我正在尝试为一个新的高尔夫球场添加一排到桌子上。该表格包含3列ID
,CourseName
和Par
。
尝试添加行时,代码仅为CourseName
和Par
设置值,因为ID
是一个标识字段。
当我单击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 :(得分:0)
<div class="coverw-block-welcome"></div>
这个动作需要像这样改变:
[HttpGet]
public PartialViewResult AddEdit(int? id)
{
return PartialView("_AddEditCourse", CourseManager.GetCourse(id));
}
如果id具有值,则GetCourse方法必须仅返回课程。
在保存时,您需要检查id == 0,因为如果是新记录,您将拥有id = 0,otherwse id&gt; 0。
编辑:
获得课程的方法必须如下:
[HttpGet]
public PartialViewResult AddEdit(int? id)
{
if(id.HasValue)
return PartialView("_AddEditCourse", CourseManager.GetCourse(id));
return PartialView("_AddEditCourse",new Course())
}
了解为什么会出现错误我需要您发布剃刀视图。