【问题标题】:Linkbutton equivalent in MVC C# RazorMVC C# Razor 中的等效链接按钮
【发布时间】:2016-07-20 02:15:10
【问题描述】:

我正在将 Web 表单转换为 MVC C# razor 项目。我想知道如何在 Razor 中获得相同的功能,它将创建一个链接并将表单汇总到同一页面中。网页表单代码是 -

<asp:LinkButton ID="lnkBtn_1" runat="server" Text='<%#Eval("Sales") %>' OnCommand="LinkButton1_Command" CommandArgument='<%#Eval("Sales") %>' ></asp:LinkButton>

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    如果您是 mvc 新手,那么您需要从 MVC 教程中检查它的基本知识:请按照以下线程将您的 Web 应用程序代码转换为 MVC

    https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx

    @Html.ActionLink("buttonText",new { controller = "ContreollerName", action = "ActionName", @sales = "你的参数" })

    然后在你的控制器中产生一个动作结果

    【讨论】:

      【解决方案2】:

      在 rezor 中有很多方法可以使用链接按钮,尝试其中任何一种 <button type="button" onclick="@("window.location.href='" +@Url.Action("ActionResult", "Controller") + "'")"> Link Button </button> <a href="@Url.Action("ActionResult", "Controller")"> Link Button </a> @Html.ActionLink("Text","ActionResult","Controller") 将表单提交到同一页面,您必须使用 Ajax Begin Form 或使用带有 ajax post 方法的简单 json 对象。像这样试试

      $("#btnSave").click(function (e) {
      
      var urlpath = '@Url.Action("ActionResult", "Controller")';
      $.ajax({
          url: urlpath ,
          type: "POST",
          data: JSON.stringify({ 'Options': someData}),
          dataType: "json",
      
          contentType: "application/json; charset=utf-8",
          success: function (data) {
              if (data.status == "Success") {
                  alert("Done");
      
              } else {
                  alert("Error occurs on the Database level!");
              }
          },
          error: function () {
              alert("An error !!!");
          }
      });
      

      【讨论】:

        【解决方案3】:

        试试这个

        @Html.ActionLink("buttonText", "ControllerAction", "YourController ", 
                 new { @sales = YourModel.Parameter}, new { @class =yourcssclass" })
        

        你的控制器

        public class YourController : Controller
        {
        public ActionResult Index()
         {
                var model = YourDataToDisplay;
        
                return View(model);
         }
        public ActionResult ControllerAction(int sales)
        {
            //.....
        }
        }
        

        您可以使用 ViewData 来定义 ButtonText。

        【讨论】:

        • 添加模型名称时出现编译错误
        • 您必须在视图顶部添加对模型的引用@model Classes.YourModel
        【解决方案4】:

        你可以写一个锚标签。但是点击锚标记通常不会提交表单,而是触发一个 HTTP GET 请求。所以你需要编写一些java脚本来进行表单提交。

        <form action="Customer/Create">
           <a href="#" id="linkToSubmit">Submit</a>
        </form>
        

        使用jQuery和一些不显眼的javascript代码在这个锚标签上绑定点击事件,

        $(function(){
          $("#linkToSubmit").click(function(e){
            e.preventDefault(); // prevent the normal link click behaviour (GET)
            $(this).closest("form").submit();
          });
        });
        

        现在您可以在Customer 控制器中执行Create 操作方法的HttpPost 操作中的代码(因为这是表单的action 设置为

        另一种选择是将提交按钮保留在表单中并设置样式,使其看起来像in this answer 解释的链接。

        【讨论】:

        • 我已经有一个 httppost 操作 - [HttpPost] public ActionResult ApReport(ApReport Ddl) { .....},我将如何在这个 httppost 中获取提交的值
        • 只要您的表单字段名称与您的 ApReport 类的属性名称匹配,模型绑定就会负责将发布的表单值映射到 Ddl 对象。
        • 我在模型类 (public string linkToSubmit { get; set; }) 和类内添加了 (string lnkBtn; lnkBtn = Request.Form["linkToSubmit"];) 但在点击链接后,它没有向控制器提交任何值,只是返回到同一页面
        • 您不需要将linkToSubmit 添加到您的视图模型类中吗?您想将表单字段值传递给您的操作方法 rite 吗?此外,您不需要阅读 Request.Form 之类的值。 MVC 模型绑定会照顾你。 我强烈建议阅读一些关于模型绑定如何工作的文章,然后进一步编写代码。开始here
        猜你喜欢
        • 2014-01-08
        • 2010-10-27
        • 2016-01-10
        • 2014-07-18
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多