最近刚刚接触MVC,自己看着msdn上面的例子自己写了个小程序,然后就遇到了不解的问题。 <ul>
<%foreach (CostList cost in (IEnumerable)ViewData.Model) { %>
<li>
日期:
<%=cost.Date.ToShortDateString() %>
<%if (cost.IsCompleted){%>
-- 花费:
<del><%=cost.Money.ToString() %></del>
已清算
登记人:
<del><%=cost.MainName %></del>
<%} %>
<%else { %>
-- 花费:
<%=cost.Money.ToString() %>
未清算
登记人:
<%=cost.MainName %>
<%} %>
<br />
<a href="/Info/More/<%=cost.CostId.ToString() %>">详细</a>
</li>
<%} %>
</ul>
上面是视图里的显示信息的代码。
问题出在点击"详细"的时候,在调用public class InfoController : Controller
{
private CostViewInfoDataContext cv = new CostViewInfoDataContext();
//
// GET: /Info/ public ActionResult More(int? CostId)
{
var list = from l in cv.CostViewInfo where l.CostId == CostId orderby l.ItemID select l;
return View(list.ToList());
} }的时候"More"所接收到的参数总是为null。
因为在程序运行时可以看到"详细"的链接URL是"/Info/More/1"。
但不知为什么"More"中的CostId的值总是为null。
恳请大神帮忙解答!万谢MVC
<%foreach (CostList cost in (IEnumerable)ViewData.Model) { %>
<li>
日期:
<%=cost.Date.ToShortDateString() %>
<%if (cost.IsCompleted){%>
-- 花费:
<del><%=cost.Money.ToString() %></del>
已清算
登记人:
<del><%=cost.MainName %></del>
<%} %>
<%else { %>
-- 花费:
<%=cost.Money.ToString() %>
未清算
登记人:
<%=cost.MainName %>
<%} %>
<br />
<a href="/Info/More/<%=cost.CostId.ToString() %>">详细</a>
</li>
<%} %>
</ul>
上面是视图里的显示信息的代码。
问题出在点击"详细"的时候,在调用public class InfoController : Controller
{
private CostViewInfoDataContext cv = new CostViewInfoDataContext();
//
// GET: /Info/ public ActionResult More(int? CostId)
{
var list = from l in cv.CostViewInfo where l.CostId == CostId orderby l.ItemID select l;
return View(list.ToList());
} }的时候"More"所接收到的参数总是为null。
因为在程序运行时可以看到"详细"的链接URL是"/Info/More/1"。
但不知为什么"More"中的CostId的值总是为null。
恳请大神帮忙解答!万谢MVC
<a href="/Info/More/<%=cost.CostId.ToString() %>">=》
<a href="/Info/More/?CostId=<%=cost.CostId.ToString() %>">
<%= Html.ActionLink("详细", "More", "Info", new { CostId = cost.CostId }, null })%>
这样用的最大好处是,会根据你定义的Route来最后生成的链接。就算有时候我们不能用Html.ActionLink, 也可以使用Url.Action来生成链接的href属性值。