簡體   English   中英

如何從服務中調用方法?

[英]How do I call my method from my service?

我有3個檔案; 一個視圖,一個控制器和一個服務。 我的服務中有一個名為“ UpdatePersoon”的方法。 我如何調用該方法? 我的按鈕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);
    }
}
}

它必須使用BestuuurEdit ActionResult,這是我的觀點。

location.href("URI")對URI指定的資源執行GET請求。 您可能想要在此處將表單的action屬性設置為@Url.Action("BestuuurEdit", "Bestuur") 默認情況下,提交form時,它將對action屬性指定的URL執行GET請求,但是如果要執行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>

您可以對以下方法進行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) {

    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM