简体   繁体   中英

No values being passed to View Model in Controller

I have a small asp project for user requests. I'm very new to ASP.NET. I've just created a new view model that looks like this.

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

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

Easy Peasy. Now in my controller my POST actionresult looks like this.

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

So the problem is when executing there is no data being passed from the form to the view model. I'm sure its just my noobishness. When I step through, every field is just null. I don't really understand where to bind to the view model. If i'm completely doing this wrong feel free to suggest.

Any help, or direction to appropriate materials is great.

<< UPDATE >> Here is my view, sorry about that.

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

Try changing viewmodel properties to use getters and setters, not sure if this is the issue but I have never seen viewmodel properties handled that way.

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

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

As the comments stated you should also post your view as there could be some things going on there as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM