如何从两列数据迭代将在ASP.Net MVC单选框 [英] How to iterate Data from two columns into single select box in ASP.Net MVC

查看:104
本文介绍了如何从两列数据迭代将在ASP.Net MVC单选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我做它老式的方式不一样使用任何模型。
我想从同一个表中获得的价格和名称列,显示选择框选项名称,每当用户选择一个选项,它的价格应该进入一个侧面的标签。它装载的名字,但不知道如何得到的价格。



任何帮助,高度赞赏。



< STRONG>控制器代码:

 字符串的ConnectionString = ConfigurationManager.ConnectionStrings [MyConnString]的ConnectionString; 
SqlConnection的康恩=新的SqlConnection(ConnectionString中);

名单,LT;字符串> listId =新的List<串GT;();
名单,LT;字符串> LISTNAME =新的List<串GT;();
名单,LT;字符串> listPrice =新的List<串GT;();


conn.Open();
查询字符串=SELECT标识,PKGNAME,价格从套餐;
的SqlCommand cmdss =新的SqlCommand(查询,康涅狄格州);
SqlDataReader的博士= cmdss.ExecuteReader();
而(dr.Read())
{
listId.Add(博士[0]的ToString());
listName.Add(DR [1]的ToString());
listPrice.Add(博士[2]的ToString());


}

conn.Close();

VAR ListNameArray = listName.ToArray();
ViewBag.ListNameArray = ListNameArray;

VAR ListPriceArray = listPrice.ToArray();
ViewBag.ListPriceArray = ListPriceArray;

返回查看();



查看代码:



 <选择> 

@foreach(在ViewBag.ListNameArray字符串项)
{
<选项> @项目< /选项>
}

< /选择>


解决方案

您应该创建一个类来表示每个包记录。

 公共类套餐
{
公众诠释标识{设置;得到; }
公共字符串名称{设置;得到; }
公共小数价格{设置;得到; }
}

现在,当你读表,创建这个类的一个列表,并添加。一个对象到这个列表为每个记录

  VAR名单=新名单,LT;包装及GT;(); 
使用(SqlDataReader的博士= cmdss.ExecuteReader())
{
,而(dr.Read())
{
变种P =新包();
p.Id = reader.GetInt32(reader.GetOrdinal(ID));
p.Name = reader.GetString(reader.GetOrdinal(包名称));
p.Price = reader.GetDecimal(reader.GetOrdinal(价格));
list.Add(对);
}
}



假设标识是int类型的价格是十进制的类型。



所以,现在创建一个视图模型你的看法

 公共类PackageVm 
{
公开名单<包装及GT;包{集;获取;}
公众诠释SelectedPackage {集;获取;}
}

现在在GET行动,创建一个这样的对象,加载软件包属性的列表,并将其发送到视图。

 公众的ActionResult的Create()
{
变种VM =新PackageVm();
vm.Packages = GetPackages();
返回查看(VM);
}



假设GetPackages方法返回包(上一个方法)。<列表/ p>

现在,使用 Html.DropDownListFor helper方法来呈现落下。同时,我们会软件包此列表存储到一个JavaScript数组,我们将在以后使用。当用户改变了下拉列表中,通过读取包阵列选定的项目值(ID)和环路和比较标识的。如果匹配,获得该项目的价格属性,并显示在标签中。

  @model PackageVm 
@using(Html.BeginForm())
{
@ Html.DropDownListFor(S = GT; s.SelectedPackage,新的SelectList(Model.Packages,ID,姓名))
<标签ID =价>测试与LT; /标签>
<输入类型=提交/>
}
@section脚本
{
$(函数(){

VAR包= @ Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject (Model.Packages));
$(#SelectedState),变化(函数(){
变种VAL = $(本).VAL();
$。每次(包,功能(A,b){
如果(b.Id == VAL){
$(#价格)HTML(b.Price);
}
});
});
});
}


Currently I am doing it in old fashioned way like without using any model. I want to get price and Name columns from same table and show the Name in Selectbox options and whenever user selects an option its price should come into a label on the side. It is loading Names but dont know how to get the price.

Any help is highly appreciated.

Controller Code:

        string ConnectionString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnectionString);

        List<string> listId = new List<string>();
        List<string> listName = new List<string>();
        List<string> listPrice = new List<string>();


        conn.Open();
        string query = "select id,pkgName,price from packages";
        SqlCommand cmdss = new SqlCommand(query, conn);
        SqlDataReader dr = cmdss.ExecuteReader();
        while (dr.Read())
        {
            listId.Add(dr[0].ToString());
            listName.Add(dr[1].ToString());
            listPrice.Add(dr[2].ToString());


        }

        conn.Close();

        var ListNameArray = listName.ToArray();
        ViewBag.ListNameArray = ListNameArray;

        var ListPriceArray= listPrice.ToArray();
        ViewBag.ListPriceArray = ListPriceArray;

        return View();

View Code:

<select>

         @foreach (string item in ViewBag.ListNameArray)
         {
           <option>@item</option>
         }

</select>

解决方案

You should create a class to represent each package record.

public class Package
{
    public int Id { set; get; }
    public string Name { set; get; }
    public decimal Price { set; get; }       
}

Now when you read the table, create a list of this class and add an object to this list for each record.

var list=new List<Package>();
using(SqlDataReader dr = cmdss.ExecuteReader())
{
   while (dr.Read())
   {
        var p = new Package();
        p.Id=reader.GetInt32(reader.GetOrdinal("Id"));
        p.Name=reader.GetString(reader.GetOrdinal("pkgName"));
        p.Price=reader.GetDecimal(reader.GetOrdinal("Price"));
        list.Add(p);
   }
}

Assuming Id is of type Int an Price is of decimal type.

So now create a view model for your view

public class PackageVm
{
  public List<Package> Packages { set;get;}
  public int SelectedPackage { set;get;}
}

Now in the GET action, create an object of this, Load the Packages property to the list and send it to the view.

public ActionResult Create()
{
  var vm=new PackageVm();
  vm.Packages = GetPackages();
  return View(vm);
}

Assuming GetPackages method returns the list of Package ( the prev method).

Now in your view, which is strongly typed to your PackageVm class, Use the Html.DropDownListFor helper method to render the dropdown. Also We will store this list of Packages to a javascript array which we will use later. When user changes the dropdown, read the selected item value ( Id) and loop through the packages array and compare the Id's. If it is matching, get the Price property of that item and show it in the label.

@model PackageVm
@using(Html.BeginForm())
{
  @Html.DropDownListFor(s=>s.SelectedPackage,new SelectList(Model.Packages,"Id","Name"))
  <label id="price">Test</label>
  <input type="submit" />
}
@section Scripts
{
   $(function() {

     var packages = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Packages));
     $("#SelectedState").change(function() {
            var val = $(this).val();
            $.each(packages, function(a, b) {
                if (b.Id == val) {
                    $("#price").html(b.Price);                        
                }
            });
     });
   });
 }

这篇关于如何从两列数据迭代将在ASP.Net MVC单选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆