我有3个文件;视图,控制器和服务。我的服务中有一种名为“UpdatePersoon'”的方法。如何在我的视图中调用该方法?我的按钮onclick如何调用该方法?我的服务是在项目的WCF端(数据库所在的位置,数据库端),我的控制器和视图位于Web端。
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);
}
}
}
控制器
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
必须使用BestuuurEdit ActionResult,这是我的观点。
答案 0 :(得分:2)
location.href("URI")
对URI指定的资源执行GET
请求。您可能希望在此处将表单的action
属性设置为@Url.Action("BestuuurEdit", "Bestuur")
。默认情况下,当提交form
时,它会对GET
属性指定的网址执行action
请求,但如果您要执行POST
请求,则可以使用表单的method
属性来显式设置。为了提交您的表单,已足够<button type="submit">
,您不需要设置onclick="location.href"
。然后,您可以通过BestuurEdit
操作为WCF服务实例化客户端,并像现在一样调用方法。
<form action="@Url.Action("BestuuurEdit", "Bestuur")" method="POST">
<!-- your input fields-->
<button type="submit">Submit</button>
</form>
答案 1 :(得分:0)
您可以对以下方法进行ajax调用:
$.ajax({
url: "[Your method URL]",
type: 'POST',
data: "Your model in json form",
contentType: 'application/json; charset=utf-8',
success: function (data) {
},
error: function (error) {
}
});