如何在文本框上绑定国家/地区代码? [英] How to bind country code on textbox ?
本文介绍了如何在文本框上绑定国家/地区代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MVC 5,其中包含View页面,
< pre > @ Html.TextBoxFor(model => model.MobileNumbers ,new {Name =MobileNumbers,@ id =iduserMobile,@ class =form-control input-sm,@ placeholder =Mobile Number,@ maxlength =15})
文本框视图如下图所示,
[ ^ ]
我尝试过:
如何在从drowpdown选择Country改变之后绑定文本框内的国家代码(例如:'+ 91')?
这是DB表结构图,
[ ^ ]
我该怎么办这个?
解决方案
参考这个样本
控制器
公共类国家
{
public int CountryCode {get;组; }
公共字符串CountryName {get;组; }
}
公共类HomeController:Controller
{
public ActionResult Index()
{
List< Country> lst = new List< Country>();
lst.Add(new Country(){CountryName =India,CountryCode = 91});
lst.Add(new Country(){CountryName =Sri lanka,CountryCode = 92});
lst.Add(new Country(){CountryName =Japan,CountryCode = 93});
ViewBag.Countries = new SelectList(lst,CountryCode,CountryName);
return View();
}
}
查看
< script>
函数changeCountry(obj){
document.getElementById('iduserMobile')。value ='+'+ obj.value;
}
< / script>
@ Html.DropDownList(Countries,(SelectList)ViewBag.Countries,Choose Countr,new {@onchange =changeCountry(this)})
@ Html.TextBoxFor (model => model.MobileNumbers,new {Name =MobileNumbers,@ id =iduserMobile,@ class =form-control input-sm,@ placeholder =Mobile Number,@ maxlength =15 })
I am working on MVC 5, there is View page that contain,
<pre>@Html.TextBoxFor(model => model.MobileNumbers, new { Name = "MobileNumbers", @id = "iduserMobile", @class = "form-control input-sm ", @placeholder = "Mobile Number", @maxlength = "15" })
Textbox view is as Image shown below,
[^]
What I have tried:
How can I bind Country code inside the textbox (eg: '+91') after select Country from drowpdown change?
Here is DB table structure image,
[^]
How can I do this?
解决方案
refer this sample
Controller
public class Country { public int CountryCode { get; set; } public string CountryName { get; set; } } public class HomeController : Controller { public ActionResult Index() { List<Country> lst = new List<Country>(); lst.Add(new Country() { CountryName = "India", CountryCode = 91 }); lst.Add(new Country() { CountryName = "Sri lanka", CountryCode = 92 }); lst.Add(new Country() { CountryName = "Japan", CountryCode = 93 }); ViewBag.Countries = new SelectList(lst, "CountryCode","CountryName"); return View(); } }
View
<script> function changeCountry(obj) { document.getElementById('iduserMobile').value = '+ ' + obj.value; } </script> @Html.DropDownList("Countries", (SelectList)ViewBag.Countries, "Choose Countr", new { @onchange = "changeCountry(this)" }) @Html.TextBoxFor(model => model.MobileNumbers, new { Name = "MobileNumbers", @id = "iduserMobile", @class = "form-control input-sm ", @placeholder = "Mobile Number", @maxlength = "15" })
这篇关于如何在文本框上绑定国家/地区代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文