Reputation: 891
I have 3 files; a view, a controller and a service. I have a method in my service called 'UpdatePersoon'. How do I call that method in my view? How does my button onclick call that method? My service is on te WCF side of the project (where the database is, the database side) and my controller and view are on the web side.
<button type="submit" id="btnSaveChanges" onclick="location.href'@Url.Action("BestuuurEdit", "UpdatePersoon")'" value="Wijzigen" class="btn btn-primary">Wijzigen</button>
Service
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;
}
}
}
Controller
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);
}
}
}
It has to use the BestuuurEdit ActionResult, that's my view.
Upvotes: 0
Views: 114
Reputation: 601
You can make a ajax call to the method like below:
$.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) {
}
});
Upvotes: 0
Reputation: 4617
location.href("URI")
performs a GET
request on the resource specified by the URI. What you might want to do here is to set your action
attribute of the form to @Url.Action("BestuuurEdit", "Bestuur")
. By default, when the form
is submitted, it will perform a GET
request to the URL specified by the action
attribute, but if you want to do a POST
request you can use the method
attribute of the form to explicitly set that. In order to get your form submitted, is enough to have the <button type="submit">
, you don't need to set onclick="location.href"
. From your BestuurEdit
action you can then instantiate a client for your WCF service and call methods on it as you already do.
<form action="@Url.Action("BestuuurEdit", "Bestuur")" method="POST">
<!-- your input fields-->
<button type="submit">Submit</button>
</form>
Upvotes: 2