0

我正在研究 mvc 项目。在我的控制器中,我从术语类调用我的存储过程,如果它返回 true ,我将返回索引页面,如果返回 false,则返回术语页面。

在术语页面中调用存储过程:

public class Accept
{

    public void Check()
    {

        using (var ctx = new termsEntities())
        {
            ctx.usp_ChkTerms(8, new ObjectParameter("Accepted", typeof(bool)));
            ctx.SaveChanges();
        }

    }
}

现在我在我的控制器中调用它:

public ActionResult App()
{

    // calling Stored procedure from Model to class            
    var accept = new Accept();
    accept.Check();

    // checking if accepted is true then return view else return another view
    AppEntities Accepted = new AppEntities();
    AppTerm user = new AppTerm();
    AppHist history = new AppHist();
    user = (from AppTerm app in Accepted.AppTerms
            where app.userID == 8
            select app).ToList().FirstOrDefault();


    if (user != null)
    {
        if (user.Accepted)
        {
            return View("Index");
        }
        else
        {

            return View("terms");
        }
    }

这是我在术语视图中使用的代码:

   @{
    ViewBag.Title = "terms";
   }
  <html>

    <body>
       <ul>

          @foreach ( var item in Model)
           {
     <div class="Page"  onclick="location.href='@Url.Action("Info", new { id = item.ID       })'">
     span class="Col1">
                            <br />
                            @item.ID
                        </span>
   <span class="Title">@item.Name</span>
    }
  </ul>
   </body>
  </html>

在这里,当条件为真时,它正在显示索引页面,但是当条件下降并且尝试显示术语页面时,我得到的对象引用未设置为对象的实例,并且错误指向 foreach 循环。那么我在这里犯了什么错误?我需要帮助..

4

2 回答 2

1

很丑,但你可以试试

<div class="Page"  onclick='location.href=&quot;@Url.Action("Info", new { id = item.ID       })&quot;'>
于 2013-09-08T19:49:36.157 回答
0
 <div class="Page"  onclick="location.href='@Url.Action("Info", new { id = item.ID       })'">

将其更改为:

 <div class="Page"  onclick="location.href='@Url.Action('LinkText','Info', new { id = item.ID       })'">

注意周围的引号Info

编辑: 为链接添加了额外的参数。

于 2013-09-08T18:51:52.247 回答