没有值传递给Controller

时间:2017-08-08 18:38:56

标签: c# asp.net asp.net-mvc

我有一个用于用户请求的小型asp项目。我是ASP.NET的新手。我刚刚创建了一个看起来像这样的新视图模型。

public class createViewModel
{
    public Change Changevm = new Change();

    public List<RequestType> rTypes = new List<RequestType>();
}

轻松的Peasy。现在在我的控制器中,我的POST动作看起来像这样。

 [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"));
        }
    }

所以问题是在执行时没有数据从表单传递到视图模型。我确定这只是我的无聊。当我单步执行时,每个字段都是空的。我真的不明白绑定到视图模型的位置。如果我完全做错了,请随时提出建议。

任何帮助或对适当材料的指导都很棒。

&LT;&LT;更新&gt;&gt; 以下是我的看法,抱歉。

@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>

1 个答案:

答案 0 :(得分:0)

尝试更改viewmodel属性以使用getter和setter,不确定这是否是问题,但我从未见过以这种方式处理viewmodel属性。

public class createViewModel
{
    public Change Changevm { get; set; }

    public List<RequestType> rTypes { get; set; }
}

正如评论所述,您也应该发布您的观点,因为那里可能会有一些事情发生。