简体   繁体   English

如何在MVC4中做出自定义删除操作结果?

[英]How can I make a custom delete action result in MVC4?

This is my model: 这是我的模型:

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; }

}

One StockLine could be related to its corresponding DeliveryNoteLine. 一个StockLine可能与其对应的DeliveryNoteLine相关。

What I want to implement is that when you delete the DeliveryNoteLine, it must also delete its corresponding StockLine . 我要实现的是,当删除DeliveryNoteLine时, 它还必须删除其对应的StockLine But I do not know how possibly doing this. 但是我不知道这样做的可能性。

This is my controller: 这是我的控制器:

/// <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);
    }
}

Any ideas?? 有任何想法吗??

I solved it this way: 我是这样解决的:

StockLinesController.cs 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 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);
    }
}

I guess there's two ways of doing this. 我猜有两种方法可以做到这一点。

1) At your API you handle this double deletion with a single request to delete on DeliveryNoteLine, or 1)在您的API上,您可以通过在DeliveryNoteLine上执行一次删除请求来处理这种双重删除,或者

2) You make two API requests to delete 1st the StockLine and 2nd the DeliveryNoteLine 2)您发出两个API请求,分别删除第一个StockLine和第二个DeliveryNoteLine

It seems by the code you're doing no.2 根据您正在执行的代码看来2

If that's the case, I think the "Resource" parameter must be different in the 1st call? 如果是这样,我认为“资源”参数在第一次调用中必须不同吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM