【发布时间】:2017-11-24 20:37:19
【问题描述】:
好的,我找到了一些类似的帖子,但是没有一点帮助我找不到解决方法(其他帖子的日期不完整)。
我有一个页面,我需要在其中显示一个包含数据库值的下拉列表(显示名称,但将键 ID 保留为值)....这个列表,以及表单的其他类型的输入。
我的模特:
public class AddOfferAnnounceModel
{
public AnnounceTypeList AnnounceTypeList {get; set; }
public int Surface { get; set; }
public int SubTypeId { get; set; }
[Required]
public decimal Price { get; set; }
[StringLength(200, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 0)]
public string AnnounceDetails { get; set; }
public bool Net { get; set; }
}
public class AnnounceTypeList
{
public int SubTypeId { get; set; }
public string TypeDesc { get; set; }
}
我的控制器 [httpGET] 操作(可能必须填充下拉列表:
// GET: Announce/Add new announce
public ActionResult AddOffer()
{
string sql = "select typeId, typeDesc from announceTypes where parentTypeId = 1";
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
cn.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
var apartmentTypeList = new List<AnnounceTypeList>();
foreach (DataRow dr in dt.Rows)
{
var apartmentType = new AnnounceTypeList
{
SubTypeId = Convert.ToInt32(dr["TypeId"]),
TypeDesc = dr["TypeDesc"].ToString()
};
apartmentTypeList.Add(apartmentType);
}
return View();
}
}
我的控制器 [httpPOST] - 从 View 返回的所有值和要插入数据库的查询在哪里
[HttpPost]
public ActionResult AddOffer(AddOfferAnnounceModel model)
{
bool isValid = true; // daca datele introduse in formularul anuntului sunt corecte si respecta regulile
if (isValid)
{
string announceDetailsItem = model.AnnounceDetails;
decimal priceItem = model.Price;
int surfaceItem = model.Surface;
int subTypeId = model.SubTypeId; // GETTING FROM DROPDOWNLIST !!!!
int netItem = Convert.ToInt32(model.Net);
DateTime dateItem = DateTime.Now;
string sql = "insert into announces (typeid, statusId, locationId, finalPrice, date, surface, net, announceDetails) values (1, 1, 1, " + priceItem + ",'" + dateItem + "'," + surfaceItem + "," + netItem + ",'" + announceDetailsItem + "')";
sql += " insert into announceOfferApartments (announceId, subTypeId, yPersonTypeId) values ((select max(announceId) from announces), 6, 4)";
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn)
{
CommandType = CommandType.Text
};
cmd.ExecuteScalar();
return RedirectToAction("OfferList", "Announce");
// daca datele au fost introduse in DB, se redirectioneaza catre Lista Anunturilor
}
}
return OfferList();
}
我的观点:
@model myApp.com.Models.AddOfferAnnounceModel
...
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Surface, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surface, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
....
我知道一个视图中不能有多个模型。我知道可能必须创建一个包含所有参数的组合类,但我不知道我必须如何从 DataTable 填充列表(或者可能是带有键和值的字典?!?)并在下拉列表中显示值,并且在Controller->ActionResult AddOffer[httpPost]中将key传给参数subTypeId。
我需要以相同的形式创建 4 5 个下拉列表,但方法可能相同....!
谢谢,希望不要收到太多的反对票:D
【问题讨论】:
标签: c# asp.net asp.net-mvc drop-down-menu