我发布了以下代码。没有在控制器端建立的参数值。
null值出现在DeleteCategory(Guid id)
。
我的模型代码是正常的,模型中没有错误。
public Guid CID { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Text)]
[Display(Name = "Category Name" )]
public string Category { get; set; }
public List<CategoryViewModel> cat;
public CategoryViewModel() {
cat = new List<CategoryViewModel>();
}
我的veiw代码在这里我将actionlink中的参数传递给控制器。它正确调用控制器,但控制器的参数值始终保持为空。
<table class="table">
<tr>
<th>
</th>
<th>
@Html.DisplayNameFor(model => model.Category)
</th>
<th></th>
</tr>
@foreach (var item in r) {
<tr>
<td>
@Html.HiddenFor(modelItem => item.CID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "DeleteCategory", "Admin", new { id = item.CID })
</td>
</tr>
我的routeconfig.cs如下。我没有编辑它。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
我的控制器代码是:这里是一个控制器,我在其中传递参数。但是id的值总是保持不变。
public PartialViewResult DeleteCategory(Guid? id)
{
CategoryViewModel cc = new CategoryViewModel();
ssp_category_ID_Result cs;
try
{
cs = new GenericList<ssp_category_ID_Result(sce.ssp_category_ID(id).GetEnumerator()).Single();
cc.Category = cs.CategoryName;
cc.CID = cs.CID;
return PartialView("_DeleteCategoryPartial", cs);
}
catch(Exception ex){
ModelState.AddModelError("",ex.Message);
}
return PartialView("_DeleteCategoryPartial");
}
[HttpPost]
public ActionResult DeleteCategory(CategoryViewModel cs)
{
try
{
sce.dsp_category(cs.CID);
ModelState.AddModelError("","Deleted");
}
catch (Exception ex) {
ModelState.AddModelError("",ex.Message);
}
return View("Category");
}
请任何人指导我为什么我能在控制器agrument中找到任何参数值
答案 0 :(得分:1)
我认为你使用错误的重载,你正试图使用:
ActionLink(HtmlHelper, String, String, Object, Object)
http://msdn.microsoft.com/en-us/library/dd492124(v=vs.118).aspx
何时应该使用:
ActionLink(HtmlHelper, String, String, String, Object, Object)
http://msdn.microsoft.com/en-us/library/dd504972(v=vs.118).aspx
所以请将您的代码更改为:
@Html.ActionLink("Delete", "DeleteCategory", "Admin", new { id = item.CID }, new{})