从DropDownList中选择一个值以能够填写字段 [英] Selecting a value from a DropDownList to be able to fill in fields
问题描述
我已经用以下命令填充了一个DropDownList: 型号:
I have populated a DropDownList with the following: Model:
namespace VAGTC.Models
{
using System;
using System.Collections.Generic;
public partial class Organization
{
public int OrganizationID { get; set; }
public string Name { get; set; }
}
控制器:
public ActionResult Index()
{
ViewBag.DropOrgID = ddp.OrganizationList();
return View();
}
ViewModel:
ViewModel:
namespace VAGTC.ViewModels
{
public class OrgDrop
{
public Organization organization { get; set; }
public IEnumerable<Organization> Organizations {get; set;}
}
}
助手:
public class DropDownPopulatorController : Controller
{
private VAGTCEntities db = new VAGTCEntities();
public SelectList OrganizationList(int selectedOrg = 0)
{
var orgQuery = from d in db.Organizations
orderby d.Name
select d;
return new SelectList(orgQuery, "OrganizationID", "Name", selectedOrg);
}
}
然后,我只需在索引视图中放入"@ Html.DropDownList("DropOrgID")"即可.
Then, I simply put "@Html.DropDownList("DropOrgID")" in my index view.
我要做的是通过根据DropDownList中选择的内容从数据库中获取数据来填充文本框(尚未实现).但是我无法弄清楚-我感觉好像是要错误地创建和填充下拉列表.
What I have to accomplish is filling in text boxes (which I have not yet implemented) by fetching data from my database depending on what is selected in the DropDownList. But I could not figure this out - I feel as if I went about creating and populating the drop down incorrectly.
我是MVC的新手,正在慢慢学习!我正在逐步进行!现在专注于仅获取SelectedValue.我尝试了多种创建和填充下拉菜单的方法,但这是对我有用的方法.我查找了用于获取SelectedValue的教程,但它似乎与我进行下拉的方式不兼容.具体来说,我找到了
I am new to MVC and I am slowly learning! I am taking it step-by-step! Focusing now on just fetching a SelectedValue. I have tried many ways of creating and populating a drop down but this is the one that worked for me. I looked up tutorials for fetching the SelectedValue but it doesn't seem to work with the way I made the drop down. Specifcally, I found the Cascading DropDownList but I could not get it to work with my drop down.
所以我的问题是:如何获取一个SelectedValue,然后将该值传递给一个控制器,然后再传递一个模型(我认为那是它的工作原理?),以便根据选择来选择数据.
So my question: How does one go about fetching a SelectedValue and then pass the value along to a controller and then a model (I think that would be how it works?) to select data based on the selection.
我们非常感谢您的帮助.
Any help is extremely appreciated.
供参考:这是我的下拉菜单
谢谢!
推荐答案
您可以在Controller中的后操作"中以以下方式获取下拉列表的选定值,
You can Get Drop-down's selected value in Post Action in Controller as,
[HttpPost]
public ActionResult Index(FormCollection frm)
{
var selectedValue = frm["DropOrgID"];
return View();
}
这篇关于从DropDownList中选择一个值以能够填写字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!