【问题标题】:Spacing Text could not be match in SQL ServerSQL Server 中的间距文本无法匹配
【发布时间】:2018-05-03 17:38:04
【问题描述】:

嗨,我是新的 ASP.NET MVC。我的 JavaScript 代码有问题。
我正在尝试将下拉列表过滤为空格文本。我在用 多个代码但结果失败请检查下图。 请帮帮我

因为我的项目时间很短,谢谢提前。

查看

@{
    ViewBag.Title = "Home Page";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script>

    $(document).ready(function () {


        $("#txt_city").change(function () {

            var id = $(this).val();

            $("#txta_address").empty();
            $.get("a_add", { city: id }, function myfunction(data) {
                var v = "<option>---Select----</option>";
                $.each(data, function (i, v1) {
                    v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";

                });
                $("#txta_address").html(v);
            });
        });
        $("#btn").on('click', function () {

            var id = $("#txta_address").val();
            $("#txtb_address").empty();
            $.get("b_add", { add: id }, function myfunction(data) {
                var v = "<option>---Select----</option>";
                $.each(data, function (i, v1) {
                    v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";

                });
                $("#txtb_address").html(v);
            });
        });


    });


</script>
<div class="row">
    <div>
        @Html.DropDownList("txt_city", null, "----Selected Country-----");
    </div>
    <div>
        <select id="txta_address"></select>
        <input id="btn" type="button" value="button" />
    </div>
    <div>
        <select id="txtb_address"></select>
    </div>
</div>

家庭控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace dropdownlist.Controllers
{
    public class HomeController : Controller
    {
        data_access_layer.db dblayer = new data_access_layer.db();

        public ActionResult Index()
        {
            city_bind();
            return View();
        }

        public void city_bind()
        {
            DataSet ds = dblayer.Get_Country();

            List<SelectListItem> countrylist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                countrylist.Add(new SelectListItem { Text = dr["city"].ToString(), Value = dr["city"].ToString() });

            }
            ViewBag.txt_city = countrylist;

        }

        public JsonResult a_add(string city)
        {
            DataSet ds = dblayer.Get_A_Add(city);
            List<SelectListItem> statelist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                statelist.Add(new SelectListItem { Text = dr["address2"].ToString(), Value = dr["address2"].ToString() });
            }
            return Json(statelist, JsonRequestBehavior.AllowGet);

        }

        public JsonResult b_add(string add)
        {
            DataSet ds = dblayer.Get_B_Add(add);
            List<SelectListItem> citylist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                citylist.Add(new SelectListItem { Text = dr["add1"].ToString(), Value = dr["add1"].ToString() });
            }
            return Json(citylist, JsonRequestBehavior.AllowGet);

        }


        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

数据库层

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace dropdownlist.data_access_layer
{
    public class db
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

        //Get Country
        public DataSet Get_Country()
        {
            SqlCommand com = new SqlCommand("select distinct(city) as city from vehicle", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;

        }
        //Get Address
        public DataSet Get_A_Add(string city)
        {
            SqlCommand com = new SqlCommand("select distinct(a_address) as address2 from vehicle where city='"+city+"'", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;

        }

        //Get Address
        public DataSet Get_B_Add(string add)
        {
            SqlCommand com = new SqlCommand("select distinct(b_address) as add1 from vehicle where a_address='"+add+"'", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;

        }
    }
}

SQL Server 数据库

enter image description here

【问题讨论】:

    标签: javascript asp.net model-view-controller


    【解决方案1】:

    尝试更新这行代码

     v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";
    

    到这里

     v += "<option value=\"" + v1.Value + "\">" + v1.Text + "</option>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      相关资源
      最近更新 更多