actionlink中的值不能为null或为空 [英] Value cannot be null or empty in actionlink

查看:90
本文介绍了actionlink中的值不能为null或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是MVC设计模式的新手。我使用Entity Framework获取记录,我想将这些记录显示为链接按钮。当我使用标签时我能够显示记录,但是当我使用@ Html.ActionLink时,我得到的值不能为null或为空。代码如下,



工作:

@foreach(模型中的var项目)

{

@ items.name


}



不工作:

@foreach(模型中的var项目)

{

@ Html.ActionLink( @ items.name,详情,主页,新对象{@ items.id})

}



先谢谢:-)



我尝试过:



上面提到的代码我试过

Hi,

I am new to MVC design pattern. I use Entity Framework to get records and i want to display those records as a Link Button. When i use tag i can able to display record but when i use @Html.ActionLink i am getting Value cannot be null or empty. The code as follows,

Working:
@foreach (var items in Model)
{
@items.name

}

Not Working:
@foreach (var items in Model)
{
@Html.ActionLink(@items.name, "Details", "Home", new object { @items.id})
}

Thanks in Advance :-)

What I have tried:

Mentioned above code was i tried

推荐答案

@Html.ActionLink(items.name, "Details", "Home", new { id = items.id }, null)





当你想把它的结果写成html时,你只需要@继续。所以它需要在Html.ActionLink前面,但不需要参数,因为你将它们作为值传递,而不是直接将它们写入html。



当传递时你使用所谓的匿名类型(新的{..}语句),并且需要格式为{propNameA = propValueA,propNameB = propbValueB}。最后,您需要为ActionLink使用正确的重载来执行您想要的操作。你想要的那个有5个参数,最后一个是你想要链接的任何html属性,但我们不需要任何,所以我们提供null作为第5个参数。如果你没有提供null,那么你只有4个参数,它使用不同的重载,其中params意味着不同的东西,你将得不到你想要的。这是一个奇怪的MVC怪癖。



You only need the "@" proceeding something when you want to write the result of it to the html. So it's needed in front of Html.ActionLink but not with the parameters as you're passing those as values, not writing them directly to the html.

When passing the attributes you use what's called an anonymous type (the new { .. } statement) and that needs to be of the format {propNameA = propValueA, propNameB = propbValueB}. Lastly you need to use the right overload for ActionLink that does what you want. The one you want has 5 params, the last one being any html attributes you want on the link, but we don't need any so we supply "null" as the 5th param. If you didn't supply that null then you only have 4 parameters and it uses a different overload where the params mean different things and you won't get what you want. It's a strange quirk of MVC.


这篇关于actionlink中的值不能为null或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆