SQLデータベースとやりとりする.NETでWebサービスを構築しようとしています。このWebサービスは、後でこのデータが表示され、やりとりされるMVCによって消費されます。WCF SQLデータベースからデータを読み込み、書き込み、編集、削除するサービス
私はデータベースを準備しました。データベースとWebサービス間の接続が確立されました。自分のMVC項目を自分のソリューションに追加しました。私は自分の作成、読み取り、更新機能が働いていますが、削除は機能しません。
削除リンクをクリックすると、自分のレコードが表示され、削除を確認するメッセージが表示されます。はいをクリックすると、削除/削除できません。助けてください。
これは私のService.csファイル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
public List<Student> GetStudent()
{
var objContext = new ContosoUniversityDataEntities();
var students = objContext.Students;
return students.ToList<Student>();
}
public string InsertStudent(string firstName, string lastName, string middleName)
{
try
{
var objContext = new ContosoUniversityDataEntities();
Student s = new Student()
{
FirstName = firstName,
LastName = lastName,
MiddleName = middleName,
EnrollmentDate = DateTime.Now
};
objContext.Students.Add(s);
objContext.SaveChanges();
return "Success";
}
catch { return "failure"; }
}
public string Update(int id, string firstName, string middleName, string lastName)
{
try
{
var objContext = new ContosoUniversityDataEntities();
var s = (from d in objContext.Students where d.StudentID == id select d).Single();
s.FirstName = firstName;
s.MiddleName = middleName;
s.LastName = lastName;
objContext.SaveChanges();
return "Success";
}
catch { return "failure"; }
}
public string Delete(int id)
{
try
{
var objContext = new ContosoUniversityDataEntities();
var s = (from d in objContext.Students where d.StudentID == id select d);
foreach(var y in s)
{
objContext.Students.Remove(y);
}
objContext.SaveChanges();
return "Success";
}
catch { return "failure"; }
}
public List<Student> InsertStudent()
{
throw new NotImplementedException();
}
public string Update(string firstName, string lastName, string middleName)
{
throw new NotImplementedException();
}
}
ですこれは私のDeleteControllerクラス
using MvcWcfApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcWcfApplication.Controllers
{
public class DeleteController : Controller
{
//
// GET: /Delete/
[HttpGet]
public ActionResult Delete(int id)
{
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
var students = obj.GetStudent();
var std = students.Where(s => s.StudentID == id).FirstOrDefault();
return View(std);
}
[HttpPost]
public ActionResult Delete(Studentdata mb)
{
if (ModelState.IsValid) //checking model is valid or not
{
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
string message = obj.Delete(mb.StudentID);
if (message == "Success")
{
ViewData["result"] = message;
ModelState.Clear(); //clearing model
return View();
}
else
{
ModelState.AddModelError("", "We are currently down");
return View();
}
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
}
}
ですこれは
@model MvcWcfApplication.ServiceReference1.Student
@{
ViewBag.Title = "Delete";
}
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Data deleted Successfully");
</script>
}
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Student</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.EnrollmentDate)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EnrollmentDate)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.FirstName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.FirstName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.LastName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.LastName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.MiddleName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.MiddleName)
</div>
</fieldset>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<p>
<input id="Submit" onclick="return confirm('Are you sure you want delete');" type="submit"
value="Delete" /> |
@Html.ActionLink("Back to List", "Index", "Db")
</p>
}
Delete.cshtml
ですこれはUpdateControllerクラス です3210これは、あなたが値なしで空のフォームを提出している私のUpdate.cshtml
@model MvcWcfApplication.ServiceReference1.Student
@{
ViewBag.Title = "Update";
}
@{
if (ViewData["resultUpdate"] != "" && ViewData["resultUpdate"] != null)
{
ViewData["resultUpdate"] = null;
<script type="text/javascript" language="javascript">
alert("Data updated Successfully");
</script>
}
}
<h2>Update</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Student</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MiddleName)
@Html.ValidationMessageFor(model => model.MiddleName)
</div>
@Html.HiddenFor(model => model.StudentID)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
を同上を置きます変数: 'catch(Exception ex){}'を呼び出し、問題の内容を調べます。 – pay