这是我的模特:
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; }
}
一个StockLine可能与其相应的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 :(得分: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)
我想有两种方法可以做到这一点。
1)在您的API中,您只需在DeliveryNoteLine上删除一个请求即可处理此双重删除,或
2)您发出两个API请求以删除第一个StockLine和第二个DeliveryNoteLine
看起来你正在做的代码没有.2
如果是这种情况,我认为第一次通话中的“资源”参数必须不同?