私は3つのファイルを持っています。ビュー、コントローラー、およびサービスです。私は私のサービスで「UpdatePersoon」というメソッドを持っています。そのメソッドを私のビューでどのように呼び出すのですか?私のボタンはどのようにそのメソッドを呼び出しますか?私のサービスはプロジェクトのWCF側(データベースはデータベース側)にあり、コントローラとビューはWeb側にあります。サービスから自分のメソッドを呼び出すにはどうすればいいですか?
<button type="submit" id="btnSaveChanges" onclick="location.href'@Url.Action("BestuuurEdit", "UpdatePersoon")'" value="Wijzigen" class="btn btn-primary">Wijzigen</button>
サービス
using System.Collections.Generic;
using System.Linq;
using WCFPlanningTool.Models.Bestuur;
using System.Data.Entity.Migrations;
using System.Web.ModelBinding;
using System.Web.Mvc;
namespace WCFPlanningTool.Services.Bestuur
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BestuurService" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select BestuurService.svc or BestuurService.svc.cs at the Solution Explorer and start debugging.
public class BestuurService : IBestuurService
{
public PlanToolEntities db = new PlanToolEntities();
public BestuurModel GetBestuurByOrganisatieId(int organisatieId)
{
BestuurModel result = null;
List<BESTUURSLID> items = db.BESTUURSLID.Include("Persoon").Include("Functie").Where(r => r.ORGANISATIE_ID.Equals(organisatieId)).ToList();
result = new BestuurModel(items);
return result;
}
public bool UpdatePersoon(PersoonModel persoon)
{
bool result = true;
db.Persoon.AddOrUpdate(persoon.GetPoco());
db.SaveChanges();
return result;
}
}
}
コントローラ
using OrgPlanTool.BestuurService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OrgPlanTool.Models.Bestuuur;
using System.Net;
using System.Data.Entity;
using System.Data;
using System.Data.Entity.Migrations;
namespace OrgPlanTool.Controllers
{
public class BestuuurController : Controller
{
public ActionResult BestuuurView()
{
BestuurService.BestuurServiceClient client = new BestuurService.BestuurServiceClient();
BestuurModel2 model = new BestuurModel2(client.GetBestuurByOrganisatieId(17));
return View(model);
}
[HttpPost]
public ActionResult BestuuurEdit()
{
BestuurService.BestuurServiceClient client = new BestuurService.BestuurServiceClient();
BestuurModel2 model = new BestuurModel2(client.GetBestuurByOrganisatieId(17));
return View(model);
}
}
}
それはそれは私の見解だ、BestuuurEditのActionResultを使用する必要があります。
あなたはビュー内のメソッドを呼び出すことはありません。 '[HttpPost]'でマークされたメソッドにフォームを送信し、そのメソッドであなたのサービスの 'UpdatePersoon()'メソッドを呼び出します(そして 'onclick =" location.href .... 'あなたからの送信ボタン。 –