我有一个用于用户请求的小型asp项目。我对ASP.NET很陌生。我刚刚创建了一个看起来像这样的新视图模型。没有值传递给控制器中的视图模型
public class createViewModel
{
public Change Changevm = new Change();
public List<RequestType> rTypes = new List<RequestType>();
}
易Peasy。现在在我的控制器中,我的POST actionresult看起来像这样。
[HttpPost]
public ActionResult Create(createViewModel _newViewModel)
{
try
{
// TODO: Add insert logic here
if(ModelState.IsValid)
{
using (UserRequestContextDataContext db = new UserRequestContextDataContext())
{
Request crObj = new Request();
crObj.Title = _createViewModel.Changevm.Title.ToString();
crObj.Description = _createViewModel.Changevm.Description.ToString();
db.Requests.InsertOnSubmit(crObj);
db.SubmitChanges();
}
}
return RedirectToAction("Create");
}
catch(Exception ex)
{
return View("Error", new HandleErrorInfo(ex,"Change","Create"));
}
}
所以问题是执行时没有数据从窗体传递到视图模型。我相信它只是我的无知。当我经过时,每个字段都是空的。我真的不明白在哪里绑定视图模型。如果我完全做了这个错误,随意建议。
任何帮助,或指导适当的材料是伟大的。
< <更新>> 这是我的看法,对不起。
@model UserRequests.ViewModels.createViewModel
@{
ViewBag.Title = "Create Change";
List<SelectListItem> listItems = new List<SelectListItem>();
for (int x = 1; x < 6; x++)
{
listItems.Add(new SelectListItem
{
Text = x.ToString(),
Value = x.ToString()
});
}
List<SelectListItem> listTypes = new List<SelectListItem>();
foreach(var item in Model.rTypes)
{
listTypes.Add(new SelectListItem
{
Text = item.Name.ToString(),
Value = item.Id.ToString()
});
}
}
<h2>Create a Request</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Fill out this form as completely as possible.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Changevm.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.EditorFor(model => model.Changevm.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Changevm.Title, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Changevm.Type, htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-3">
@Html.DropDownListFor(model => model.Changevm.Type, listTypes ,"-- Choose One -- ", new { htmlAttributes = new { @class = "" } })
@Html.ValidationMessageFor(model => model.Changevm.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Changevm.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.TextAreaFor(model => model.Changevm.Description, new { @class = "form-control", @rows = 5, @id = "editor1" })
@Html.ValidationMessageFor(model => model.Changevm.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Changevm.SubmitDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.EditorFor(model => model.Changevm.SubmitDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Changevm.SubmitDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Changevm.BusinessUnit, htmlAttributes: new { @class = "control-label col-md-2", })
<div class="col-md-5">
@Html.EditorFor(model => model.Changevm.BusinessUnit, new { htmlAttributes = new { @class = "form-control", @placeholder = ModelMetadata.FromLambdaExpression(x => x.Changevm.BusinessUnit, ViewData).Watermark } })
@Html.ValidationMessageFor(model => model.Changevm.BusinessUnit, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Changevm.ModuleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.EditorFor(model => model.Changevm.ModuleName, new { htmlAttributes = new { @class = "form-control", @placeholder = ModelMetadata.FromLambdaExpression(x => x.Changevm.ModuleName, ViewData).Watermark, @style = "color:black"} })
@Html.ValidationMessageFor(model => model.Changevm.ModuleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Changevm.UrgencyNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.DropDownListFor(model => model.Changevm.UrgencyNum, listItems, "-- Urgency Level (1 = Least Urgent) -- ", new { htmlAttributes = new { @class = "" } })
@Html.ValidationMessageFor(model => model.Changevm.UrgencyNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-5">
<button type="reset" class="btn btn-default">Cancel</button>
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Home", "Index", "Home")
</div>
“视图”是什么样的? '.cshtml'部分。 –
您需要在视图中绑定模型。我们需要查看视图代码以查看发生了什么。 – HarvP
是否在视图中指定模型?作为'@model Test.Models.createViewModel' –