siobeh
siobeh

Reputation: 31

Asp.net core Model binding not displaying value after Post Request

I'm new with asp.net core mvc. What I'm trying to do is to bind my viewmodel to my view. In the Get Request it is working fine, but after the Post Request and pass it to the view, though my viewmodels have values, it still display nothing.

This is what I have done.

 [HttpGet]
    public IActionResult Tito()
    { 

        return View();
    }


    [HttpPost]
    public IActionResult Tito(AttendanceViewModel model, string submit)
    {      
        model.AccountModelId = HttpContext.Session.GetString("myaccountid");
        model.IsTimeIn = submit == "timein" ? true : false;
        model.DateFrom = DateTime.Now;
        Tuple<bool,DateTime> result = this.service.LogAttendance(model);

        if (result.Item1 == true)
        {                
            return View(model);
        }            

        return View();
    }

In my view

 @model TESTCOREMVC.VIEWMODELS.Attendance.AttendanceViewModel

 <form asp-action="Tito">

                <div class="form-body">
                    <div class="card-body">
                        <div class="row pt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label asp-for="DateFrom" class="control-label" style="font-weight:bold">Time - In</label>                                        
                                    <input asp-for="@Model.DateFrom" readonly type="text" id="timein" class="form-control" placeholder="You don't have a time - in yet.">
                                    @*<small class="form-control-feedback"> This is inline help </small>*@
                                    <br /><br />
                                    <button name="submit" value="timein" type="submit" class="btn btn-info" id="btntimein"> <i class="mdi mdi-login-variant"></i> Time - In</button>                                        
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label asp-for="DateTo" class="control-label" style="font-weight:bold">Time - Out</label>
                                    <input asp-for="@Model.DateTo" readonly type="text" id="timeout" class="form-control" placeholder="You don't have a time - out yet.">
                                    @*<small class="form-control-feedback"> This is inline help </small>*@
                                    <br /><br />
                                    <button name="submit" value="timeout" type="submit" class="btn btn-danger" id="btntimeout"> <i class="mdi mdi-logout-variant"></i> Time - Out</button>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
            </form>

It should display the datetime after post request, but it doesn't.

Upvotes: 2

Views: 1673

Answers (1)

foremaro
foremaro

Reputation: 178

Make sure your model's inputs have a "name" attribute. Try adding:

name="Model.PropertyName" to your input tag

Or try using the included helper methods such as @Html.EditorFor() https://www.tutorialsteacher.com/mvc/htmlhelper-editor-editorfor

Upvotes: 2

Related Questions