这是我的模型:如何在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);
}
}
任何想法?
我想做你所提到的,但我不能够获得StockLine的DeliveryNoteLine_Id属性,所以即使更改资源参数也无法删除它eter,这应该是“StockLines”。 – Kutyel
难道你不能通过第一个模型的导航属性? –
我不行。如果我尝试浏览delete action结果方法中的模型属性,它只有两个:Id和DateCreated。如果我只能从stocklines控制器中检索到股票行的参考或交货单行,我可以毫无问题地将其删除,但是我不能。 – Kutyel