【问题标题】:Use same input text for two button using ASP.NET MVC使用 ASP.NET MVC 为两个按钮使用相同的输入文本
【发布时间】:2016-02-11 16:28:50
【问题描述】:

我想用相同的文本去不同的视图。目前我设置了两个视图,一个是 PlaceInformation,另一个是 Google Map View。如何设置条件以使用 HTML Beginfrom 进行查看。我想在这里使用 @using (Html.BeginForm("GoogleMapView", "Home"))。我的代码示例如下所示-

@using (Html.BeginForm("PlaceInformation", "Home"))
{
    <div class="wrapper wrapper-content">

        <div class="row">
            <div class="col-sm-12">
                @Html.TextBoxFor(m =>m.Name)
                <label for="somevalue">City Name</label>
                    <div class="input-group-btn">
                        <button class="btn btn-lg btn-primary" type="submit">Search</button>
                    </div>
                  <div class="input-group-btn">
                     <button class="btn btn-lg btn-primary" type="submit">Map View</button>
                  </div>
            </div>
        </div>

    </div>
  }

这就是我修改代码的方式。但它不起作用。

<form id="myForm">

<div class="wrapper wrapper-content">

    <div class="row">
        <div class="col-sm-12">
            @Html.TextBoxFor(m => m.Name)
            <label for="somevalue">City Name</label>

            <div class="input-group-btn">
                <button id="searchBtn" class="btn btn-lg btn-primary" type="submit">Search</button>
            </div>
            <div class="input-group-btn">
                <button id="mapViewBtn" class="btn btn-lg btn-primary" type="submit">Map View</button>
            </div>


        </div>
    </div>

</div>

  <script> {
   $("#searchBtn").on("click", function (event) {
    event.preventDefault();

    $.ajax({
        type: "POST",
        url: '/home/placeinformation',
        data: $("#myForm").serialize(), // serializes the form's elements.
        success: function (data) {
            //here you will get the result from the Controllers, like a partial view or you can do a redirect to another view if form post is successful.
        },
        error: function (xhr, status, error) {
           //Handle any errors here
        }
    });
  });
 }
 </script>

<script>{
  $("#mapViewBtn").on("click", function (event) {
    event.preventDefault();

    $.ajax({
        type: "POST",
        url: '/home/GoogleMap',
        data: $("#myForm").serialize(), // serializes the form's elements.
        success: function (data) {
            //here you will get the result from the Controllers, like a partial view or you can do a redirect to another view if form post is successful.
        },
        error: function (xhr, status, error) {
           //Handle any errors here
        }
    });
   });
 }
 </script>

我的 GoogleMap 控制器是-

    public ActionResult GoogleMap(City objCityModel)
     {
        string name = objCityModel.Name;
        ViewBag.Title = name;

        var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/POI_Json/" + name + ".json"));

        RootObject json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(ReadJson);
        List<Poi> mycities = new List<Poi>();

        foreach (var item in json.poi)
        {
            Poi obj = new Poi()
            {
                Name = item.Name,
                Shorttext = item.Shorttext,
                GeoCoordinates = item.GeoCoordinates,
                Images = item.Images,

            };
            mycities.Add(obj);
        }

        ViewBag.Cities = mycities;

        return View();

    }

获取名称-

[HttpPost]
    public ActionResult Index(City objCityModel)
    {
        string name = objCityModel.Name;
        return View();
    }

在 My PLace 信息中,我使用与 GoogleMap 视图相同的数据

 public ActionResult PlaceInformation(City objCityModel)
    {

        string name = objCityModel.Name;
        ViewBag.Title = name;

        var ReadJson = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/POI_Json/" + name + ".json"));

        RootObject json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(ReadJson);
        List<Poi> mycities = new List<Poi>();

        foreach (var item in json.poi)
        {
            Poi obj = new Poi()
            {
                Name = item.Name,
                Shorttext = item.Shorttext,
                GeoCoordinates = item.GeoCoordinates,
                Images = item.Images,

            };
            mycities.Add(obj);
        }

        ViewBag.Cities = mycities;

        return View();
    }

【问题讨论】:

  • using (Html.BeginForm("PlaceInformation", "Home", new { Id = myForm }))// 显示错误应该是 using (Html.BeginForm("PlaceInformation", "Home", new { Id = "myForm" }))// 或
    然后用结束标签替换闭合手镯'}'
  • @MagnusKarlsson:现在错误消失了,但两个按钮都在同一个 PalceInformation 视图中
  • 点击地图视图按钮不会得到地点信息的调用?
  • 是的,它转到 PlaceInformation。但我需要 Home/GoogleMap
  • GoogleMap 操作上没有 [HttpPost]?

标签: html asp.net asp.net-mvc view


【解决方案1】:

这只会生成一个 html 表单,它是决定表单发布位置的表单元素。换句话说,没有办法根据单击的按钮将此表单发布到不同的控制器操作。但是,当然还有其他方法。我会像这样使用 jQuery 绑定并发布两个发布按钮:

将 .cshtml 更改为:

<form id="myForm"> 
        @Html.TextBoxFor(m => m.Name)
        <label for="somevalue">City Name</label>

        <div class="input-group-btn">
            <button id="searchBtn" class="btn btn-lg btn-primary" type="submit">Search</button>
        </div>
        <div class="input-group-btn">
            <button id="mapViewBtn" class="btn btn-lg btn-primary" type="submit">Map View</button>
        </div>
</form>

为按钮添加 id:

 <div class="input-group-btn">
     <button id="searchBtn" class="btn btn-lg btn-primary" type="submit">Search</button>
 </div>
 <div class="input-group-btn">
     <button id="mapViewBtn" class="btn btn-lg btn-primary" type="submit">Map View</button>
 </div>

脚本:

$("#searchBtn").on("click", function (event) {
        event.preventDefault();

        $.ajax({
            type: "POST",
            url: '/home/placeinformation',
            data: $("#myForm").serialize(), // serializes the form's elements.
            success: function (data) {
                //here you will get the result from the Controllers, like a partial view or you can do a redirect to another view if form post is successful.
            },
            error: function (xhr, status, error) {
               //Handle any errors here
            }
        });
    });
}

第二个脚本(我更改了您绑定到的按钮和您要调用的控制器操作。

    $("#mapViewBtn").on("click", function (event) {
        event.preventDefault();

        $.ajax({
            type: "POST",
            url: '/home/urlToTheOtherAction,
            data: $("#myForm").serialize(), // serializes the form's elements.
            success: function (data) {
                //here you will get the result from the Controllers, like a partial view or you can do a redirect to another view if form post is successful.
            },
            error: function (xhr, status, error) {
               //Handle any errors here
            }
        });
    });
}

【讨论】:

  • 你能告诉我这里的Id=myForm是什么
  • 因此,razor 语法(cshtml 文件中的 C#)仅创建/渲染您可以在浏览器中看到的 html。 id 属性将生成一个连接到表单元素的 id 属性。它是独一无二的,我们需要它来处理数据: $("#myForm").serialize() ajax 调用中的行才能工作。该行将根据 id 获取您的表单并将其发送到服务器,就像单击其中一个按钮一样,但我们有效地阻止了两个脚本的第一行。
  • @using (Html.BeginForm("PlaceInformation", "Home", new { Id = myForm})) 在您的浏览器中会变成这样:
    这样您现在就可以真正简化代码并将其更改为
  • Myform 显示错误。它告诉 MyForm 在当前文件夹或位置中不存在
  • 您能否再解释一下或修改您的代码。我很新处理这种情况。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多