2013-10-08 96 views
2

這是我的模型:如何在MVC4中創建自定義刪除操作結果?

public class StockLine : Keyed 
{ 
    /.../ 

    /// <summary> 
    /// Reference to the delivery note line that created the current stock line. 
    /// </summary>  
    [Navigation] 
    [Display(ResourceType = typeof(Resources.ApplicationResources), Name = "DeliveryNoteLine")] 
    public virtual DeliveryNoteLine DeliveryNoteLine { get; set; } 

} 

一個料線可能與它對應的DeliveryNoteLine。

我想實現的是,當您刪除DeliveryNoteLine,它也必須刪除其相應的StockLine。但我不知道如何做到這一點。

這是我的控制器:

/// <summary> 
/// Returns the default Delete view for the TEntity object. 
/// </summary> 
/// <param name="id">Id of the TEntity object to delete.</param> 
/// <returns>Redirection to the Index action if an error occurred, the Delete View otherwise.</returns> 
public virtual ActionResult Delete(string id) 
{ 
    var request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.GET) { RequestFormat = RestSharp.DataFormat.Json } 
     .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
    var response = Client.Execute(request); 

    // Deserialize response 
    var model = DeserializeResponse<TEntity>(response); 
    HandleResponseErrors(response); 

    if (Errors.Length == 0) 
     return View(model); 
    else 
    { 
     ViewBag.Errors = Errors; 
     return RedirectToAction("Index"); 
    } 
} 

/// <summary> 
/// Handles the POST event for the Delete action. 
/// </summary> 
/// <param name="id">Id of the TEntity object to delete.</param> 
/// <param name="model">TEntity object to delete.</param> 
/// <returns>Redirection to the Index action if succeeded, the Delete View otherwise.</returns> 
[HttpPost] 
public virtual ActionResult Delete(string id, TEntity model) 
{ 
    var request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.DELETE) { RequestFormat = RestSharp.DataFormat.Json } 
     .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
    var response = Client.Execute(request); 

    // Handle response errors 
    HandleResponseErrors(response); 

    if (Errors.Length == 0) 
     return RedirectToAction("Index"); 
    else 
    { 
     request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.GET) { RequestFormat = RestSharp.DataFormat.Json } 
      .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
     response = Client.Execute(request); 
     model = DeserializeResponse<TEntity>(response); 

     ViewBag.Errors = Errors; 
     return View(model); 
    } 
} 

任何想法?

回答

0

我解決這樣說:

StockLinesController.cs

/// <summary> 
/// Service returning API's StockLine that matches the given DeliveryNote id. 
/// </summary> 
/// <param name="DeliveryNoteLineId">The id of the DeliveryNoteLine that created the StockLine</param> 
/// <returns>Returns the StockLine created by the given DeliveryNoteLine</returns> 
public ActionResult GetStockLine(string DeliveryNoteLineId) 
{ 
    // Only perform the request if the data is outdated, otherwise use cached data. 
    if (DateTime.Now.AddMinutes(-10) > _cacheStockLines_lastCall.GetValueOrDefault(DateTime.MinValue)) 
    { 
     var request = new RestSharp.RestRequest("StockLines/Get", RestSharp.Method.GET) { RequestFormat = RestSharp.DataFormat.Json }; 
     var response = Client.Execute(request); 
     _cacheStockLines = DeserializeResponse<List<StockLine>>(response); 
     _cacheStockLines_lastCall = DateTime.Now; 
    } 

    // Return the stock line created by the delivery note line introduced my parameter 
    var ret = _cacheStockLines 
     .Where(x => (x.DeliveryNoteLine != null && x.DeliveryNoteLine.Id == DeliveryNoteLineId)) 
     .Select(x => new { label = "ID", value = x.Id }); 

    return Json(ret, JsonRequestBehavior.AllowGet); 
} 

DeliveryNoteLinesController.cs

/// <summary> 
/// Handles the POST event for the Delete action. 
/// </summary> 
/// <param name="id">Id of the TEntity object to delete.</param> 
/// <param name="model">TEntity object to delete.</param> 
/// <returns>Redirection to the Index action if succeeded, the Delete View otherwise.</returns> 
[HttpPost] 
public override ActionResult Delete(string id, DeliveryNoteLine model) 
{ 
    //This code deletes the StockLine 
    var stocks_request = new RestSharp.RestRequest("GetStockLine?DeliveryNoteLineId={id}", RestSharp.Method.GET) { RequestFormat = RestSharp.DataFormat.Json } 
     .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
    var stocks_response = Client.Execute(stocks_request); 
    var stockline = DeserializeResponse<StockLine>(stocks_response); 
    var reqdelstk = new RestSharp.RestRequest("StockLine?id={id}", RestSharp.Method.DELETE) { RequestFormat = RestSharp.DataFormat.Json } 
     .AddParameter("id", stockline.Id, RestSharp.ParameterType.UrlSegment); 
    var resdelstk = Client.Execute(reqdelstk); 

    //This code deletes the DeliveryNoteLine 
    var request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.DELETE) { RequestFormat = RestSharp.DataFormat.Json } 
     .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
    var response = Client.Execute(request); 

    // Handle response errors 
    HandleResponseErrors(response); 

    if (Errors.Length == 0) 
     return RedirectToAction("Index"); 
    else 
    { 
     request = new RestSharp.RestRequest(Resource + "?id={id}", RestSharp.Method.GET) { RequestFormat = RestSharp.DataFormat.Json } 
      .AddParameter("id", id, RestSharp.ParameterType.UrlSegment); 
     response = Client.Execute(request); 
     model = DeserializeResponse<DeliveryNoteLine>(response); 

     ViewBag.Errors = Errors; 
     return View(model); 
    } 
} 
-1

我想這有兩種方法。

1)在你的API,你處理這雙重缺失與單個請求刪除DeliveryNoteLine,或

2)你讓兩個API請求刪除1日料線和2號DeliveryNoteLine

似乎通過你正在做的代碼2號

如果是這樣的話,我認爲第一次調用中的「資源」參數必須不同?

+0

我想做你所提到的,但我不能夠獲得StockLine的DeliveryNoteLine_Id屬性,所以即使更改資源參數也無法刪除它eter,這應該是「StockLines」。 – Kutyel

+0

難道你不能通過第一個模型的導航屬性? –

+0

我不行。如果我嘗試瀏覽delete action結果方法中的模型屬性,它只有兩個:Id和DateCreated。如果我只能從stocklines控制器中檢索到股票行的參考或交貨單行,我可以毫無問題地將其刪除,但是我不能。 – Kutyel

相關問題