【问题标题】:how to apply action link to html block如何将操作链接应用于 html 块
【发布时间】:2014-08-17 19:01:16
【问题描述】:
<div class="thumbnail" style="height: 70px; background-color: #EEEDED">
   <div style="font-weight: bold;font-size: 20px;text-align: center">
     @Html.Label(exam.ExamName,new{@style="cursor: pointer;color: #FF8627 "})
    </div> 
    <div style="font-size: 15px;text-align: center;color: #505050">
      @Html.Label("Total Tests: " + exam.TestInfoes.Count(), new{@style="cursor: pointer"})
   </div>
</div>

我有上面的 HTML 块,我想把整个块放在我的 mvc4 应用程序中。 我们可以在标签之间使用maually编写整个块来做到这一点,但我也想在链接中传递一些值,这些值将为标签创建

 <a href="~/Exam/SingleExam/" + some value from my model>

我们怎么做?

另外,我们如何使用 ActionLink 实现这一点?

【问题讨论】:

    标签: c# html asp.net-mvc-4 html.actionlink


    【解决方案1】:

    您可以使用PartialsEditor/Display Templates 来做到这一点

    要渲染partial,您可以使用:

    @Html.RenderPartial("partialname", model); 
    

    可以在这里找到一篇文章:

    http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

    要使用模板,您可以使用自定义的Html.DisplayFor 模板。

    这里有一篇关于这个主题的文章:

    http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx

    模板主要是模板必须匹配对象的classname

    使用这两种方法,您都可以传递您的模型。

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我创建了这样的链接

      <a href="~/Exam/SingleExam/@exam.Id">
        <div class="thumbnail" style="height: 70px; background-color: #EEEDED">
           <div style="font-weight: bold;font-size: 20px;text-align: center">
              @Html.Label(exam.ExamName,new{@style="cursor: pointer;color: #FF8627 "})
            </div> 
            <div style="font-size: 15px;text-align: center;color: #505050">
              @Html.Label("Total Tests: " + exam.TestInfoes.Count(), new{@style="cursor: pointer"})
            </div>
        </div>
      </a>
      

      我没有使用操作链接,而是使用了 html 标记并使用 @exam.id 传递了 id,这为我创建了正确的 url。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-14
        • 1970-01-01
        相关资源
        最近更新 更多