我正在使用MVC开发一个项目,但是还集成了ViewModel层。我正在使用两个不同的数据表。第一个称为用户。那有很多不同的属性。第二个称为UserProfile。里面只有用户名电子邮件和密码。这些表通过“用户编号”字段在数据库中链接。
我有一个视图,该视图允许创建用户,以及有关将使用下拉列表选择的商店定义,StoreOwnerDefinition,商店区域定义和商店类型定义的信息。如何从视图的下拉列表中选择所选择的信息到SQL数据库中。
这是我在Controller中拥有的:
public ActionResult UserCreateNew()
{
DataTable dt = new DataTable();
// Open UserStoreDefinition table and create a List.
dt = new DataTable();
string constr =
string strSQL =
using (DataAccess da = new DataAccess(constr))
{
dt = da.GetDataTable(strSQL, CommandType.Text);
}
// Convert a DataTable to a list.
m.lstStoreDefinition = LinqExtensions.ToListof<UserStoreDefinition>(dt);
dt = new DataTable();
//strSQL = "StoreOwnerDefinitions";
strSQL = "SELECT distinct StoreOwnerName, StoreOwnerNumber FROM UserStoreDefinition"
+ " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")"
+ " AND (UserStoreDefinition.StoreOwnerNumber <> -1)";
using (DataAccess da = new DataAccess(constr))
{
dt = da.GetDataTable(strSQL, CommandType.Text);
}
m.lstStoreOwnerDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);
dt = new DataTable();
//strSQL = "StoreRegionDefinitions";
using (DataAccess da = new DataAccess(constr))
{
dt = da.GetDataTable(strSQL, CommandType.Text);
}
m.lstStoreRegionDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);
dt = new DataTable();
//strSQL = "StoreTypeDefinitions";
strSQL = "SELECT distinct StoreTypeName, StoreTypeNumber FROM UserStoreDefinition"
+ " WHERE (UserStoreDefinition.UserNumber = " + m.UserNumber + ")"
+ " AND (UserStoreDefinition.StoreTypeNumber <> -1)";
using (DataAccess da = new DataAccess(constr))
{
dt = da.GetDataTable(strSQL, CommandType.Text);
}
m.lstStoreTypeDefinitions = LinqExtensions.ToListof<UserStoreDefinition>(dt);
# endregion
return View(m);
}
```
In my View Model I have:
```
and in the View I have:
```
<div class="spacerBody">
<h2 class="admin-home-link orange-titles orange-titles-large">@Html.ActionLink("Create Users", "AdminIndex")</h2>
<div class="to-link navlinks"> @Html.ActionLink("Users", "UserList") | @Html.ActionLink("Manage User Role", "RoleManageUser")</div>
<p> </p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Registration Form</legend>
<ol class="comtrexBlue-text">
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
</ol>
<button class="btn larger" type="submit">Create</button>
</fieldset>
}
<div class="form-group">
<div class="input-group-btn">
<div class="btn-group btn-group-justified" data-toggle="buttons">
<label class="btn btn-primary">
@Html.DropDownList
</label>
<label class="btn btn-primary">
@Html.DropDownList
</label>
<label class="btn btn-primary">
@Html.DropDownList
</label>
<label class="btn btn-primary">
@Html.DropDownList </label>
</div>
</div>
</div>
</div>
我想我的第一步是从视图中获取下拉信息。不执行存储过程并传递参数不是。
答案 0 :(得分:1)
您需要将表单发布回控制器,并传递表单数据:
1)在BeginForm中设置动作,控制器和方法
@Html.BeginForm("UserCreateNew", "CONTROLLERNAMEHERE", FormMethod.Post)
2)修改UserCreateNew操作以将表单数据作为UserCreateNewViewModel
public ActionResult UserCreateNew(UserCreateNewViewModel userData)
{
userData然后应在表单中输入所有数据。