【发布时间】:2016-05-19 08:17:04
【问题描述】:
我有 3 个文件;一个视图、一个控制器和一个服务。我的服务中有一个名为“UpdatePerson”的方法。在我看来如何调用该方法?我的按钮 onclick 如何调用该方法?我的服务在项目的 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);
}
}
}
它必须使用 BestuurEdit ActionResult,这是我的观点。
【问题讨论】:
-
您不会在视图中调用该方法。您将表单提交给标有
[HttpPost]的方法,并在该方法中调用服务的UpdatePersoon()方法(并且您需要从提交按钮中删除onclick="location.href....。
标签: c# asp.net-mvc wcf wcf-data-services