如何在视图中显示模态数据 [英] How to show modal data in view
问题描述
我想从数据库中获取数据,将值传递给modal。并在视图中显示模态数据。 (不使用数据视图,数据库等)
im初学者
< b>我尝试过:
@model MvcDynamicMenuBar.Models.HomePagecs
@ {
ViewBag.Title =索引;
// string ITEM_CODE = Model.ITEM_CODE;
// string ITEM_DESC = Model.ITEM_DESC;
}
@using(Html.BeginForm())
{
< h2> Index< / h2>
@ Html.LabelFor(model => model.ITEM_CODE)
@ Html.LabelFor(model => model.ITEM_DESC)
}
namespace MvcDynamicMenuBar.Models
{
public class HomePagecs
{
public String ITEM_CODE {get;组; }
public String ITEM_DESC {get;组; }
}
}
public ActionResult Index()
{
try
{
objHomePagecs = new HomePagecs();
String connstr = ConfigurationManager.AppSettings [ConnectionString];
OracleConnection STR =新的OracleConnection(connstr);
STR.Open();
string command =SELECT ITEM_CODE,ITEM_DESC FROM INV_ITEM_MASTER WHERE ITEM_ID ='16048';
OracleCommand cmd = new OracleCommand(command,STR);
OracleDataAdapter sda = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
objHomePagecs.ITEM_CODE = dt.Columns [ITEM_CODE]。ToString();
objHomePagecs.ITEM_DESC = dt.Columns [ITEM_DESC]。ToString();
}
catch(exception ex)
{
}
return View();
}
结果是
ITEM_CODEITEM_DESC
网页值未来
请参阅LabelFor的文档,您会看到它使用您的模型属性的DisplayName属性没有设置,所以它使用的是属性名称。 LabelFor用于为您的属性提供描述性名称。如果您的模型是这样的话
public class HomePagecs
{
[DisplayName( Code )]
public 字符串 ITEM_CODE {获得跨度>; set ; }
[DisplayName( 描述)]
public 字符串 ITEM_DESC { get ; set ; }
}
然后你会得到代码和描述。然而你真正想要的是物业的价值,而不是物业名称所以只需使用
@ Model.ITEM_CODE
@ Model.ITEM_DESC
您的其他显示isssue只是一个HTML问题,HTML不会将ASCII换行符视为换行符,您需要明确说明你希望如何分离数据
@ Model.ITEM_CODE< br />
@ Model.ITEM_DESC
这将在数据之间进行HTML中断,因此它位于不同的行上。
这是所有令人难以置信的基本MVC内容,我强烈建议您阅读一本关于MVC的书,或至少阅读一些关于入门的教程(谷歌mvc音乐商店)
i want to fetch data from the database, pass the value to modal. and show modal data in view. (without using dataview, databag etc)
i m beginner
What I have tried:
@model MvcDynamicMenuBar.Models.HomePagecs @{ ViewBag.Title = "Index"; //string ITEM_CODE = Model.ITEM_CODE; //string ITEM_DESC = Model.ITEM_DESC; } @using (Html.BeginForm()) { <h2>Index</h2> @Html.LabelFor(model => model.ITEM_CODE) @Html.LabelFor(model => model.ITEM_DESC) }
namespace MvcDynamicMenuBar.Models { public class HomePagecs { public String ITEM_CODE { get; set; } public String ITEM_DESC { get; set; } } }
public ActionResult Index() { try { objHomePagecs = new HomePagecs(); String connstr = ConfigurationManager.AppSettings["ConnectionString"]; OracleConnection STR = new OracleConnection(connstr); STR.Open(); string command = "SELECT ITEM_CODE,ITEM_DESC FROM INV_ITEM_MASTER WHERE ITEM_ID='16048'"; OracleCommand cmd = new OracleCommand(command, STR); OracleDataAdapter sda = new OracleDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); objHomePagecs.ITEM_CODE = dt.Columns["ITEM_CODE"].ToString(); objHomePagecs.ITEM_DESC = dt.Columns["ITEM_DESC"].ToString(); } catch(Exception ex) { } return View(); }
result is
ITEM_CODEITEM_DESC
in webpage value is not coming
Consult the documentation for LabelFor and you'll see it uses the DisplayName attribute of your model properties which you are not setting so it is using the property name instead. LabelFor is to give descriptive names to your properties. If your model was like this
public class HomePagecs { [DisplayName("Code")] public String ITEM_CODE { get; set; } [DisplayName("Description")] public String ITEM_DESC { get; set; } }
then you'd get "Code" and "Description" instead. However what you actually want is the value in the property, not the property name so simply use
@Model.ITEM_CODE @Model.ITEM_DESC
Your other display isssue is simply an HTML issue, HTML does not treat ASCII line breaks etc as line breaks, you need to explicitly say how you want the data separated
@Model.ITEM_CODE<br/> @Model.ITEM_DESC
That will do an HTML break between the data so it is on different lines.
This is all incredibly basic MVC stuff, I strongly recommend you get a book on MVC or at least go through some tutorials on getting started (google "mvc music store")
这篇关于如何在视图中显示模态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!