2016-08-11 2 views
0
метод

действия:MVC - На посту модель вид не получает заселена

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Register(RegisterViewModel blahblah) 
    { 
     HttpPostedFileBase uploadFile; 
     if (ModelState.IsValid) 
     { 
      if (blahblah != null) 
      { 
       var obj = new tblPersonalDetail() 
       { 
        FirstName = blahblah.FirstName, 
        LastName = blahblah.LastName, 
        Title = blahblah.Title, 
        Address = blahblah.Address, 
        Suburb = blahblah.Suburb, 
        HomePhone = blahblah.HomePhone, 
        Mobile = blahblah.Mobile, 
        Email = blahblah.Email, 
        EmergencyName = blahblah.EmergencyContactName, 
        EmergencyPhone = blahblah.EmergencyContactPhone, 
        EmergencyEmail = blahblah.EmergencyContactEmail, 
        EmergencyRelation = blahblah.EmergencyContactRelation, 
        DrivingLicenceExpiryDate = blahblah.DrivingLicenceExpiryDate, 
        DrivingLicenceNo = blahblah.DrivingLicenceNo, 
        DateofBirth = blahblah.DateofBirth 
       }; 


       //if (uploadFile != null && !string.IsNullOrEmpty(uploadFile.FileName)) 
       //{ 
       // uploadFile.SaveAs(Server.MapPath([email protected]"~\Content\Images\{uploadFile.FileName}")); 
       // obj.ScannedImageLocation = ([email protected]"~\Content\Images\{uploadFile.FileName}"); 
       //} 
       db.tblPersonalDetails.Add(obj); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
     } 

     return View(blahblah); 
    } 

--registerviewmodel

public class RegisterViewModel 
{ 
    public string Title; 
    public string FirstName; 
    public string LastName; 
    public string Address; 
    public string Suburb; 
    public string HomePhone; 
    public string Mobile; 
    public string Email; 
    public string EmergencyContactName; 
    public string EmergencyContactRelation; 
    public string EmergencyContactPhone; 
    public string EmergencyContactEmail; 
    public string DrivingLicenceNo; 
    // [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")] 
    public DateTime DrivingLicenceExpiryDate; 
    public string DrivingLicenceImage; 
    // [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:yyyy-MM-dd}")] 
    public DateTime DateofBirth; 
    public string Notes; 
    public string NextAppointment; 
    public string Name 
    { 
     get 
     { 
      return $"{FirstName} {LastName}"; 
     } 
    } 


} 

модели представления на пост все нуль. если я использую класс модели tblPersonalDetail, сгенерированный с помощью entityframework, а затем изменим ссылку в представлении (Register.cshtml), он публикует данные. Однако не с пользовательской моделью просмотра. --Register.cshtml

@model Leo.ViewModel.RegisterViewModel 
@{ 
ViewBag.Title = "Register New User"; 
} 
@using (Html.BeginForm("Register", "Home", FormMethod.Post)) 
{ 
@Html.AntiForgeryToken() 


<div class="row"> 
    <div class="col-md-8"> 
     <div class="panel"> 

      <div class="panel-heading tabbable" tabindex="0"><h1>Personal Details</h1></div> 

      <div class="panel-body"> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 

         @Html.LabelFor(model => model.Title, "Title", htmlAttributes: new { @class = "control-label" }) 
         <select class="form-control" id="Title"> 
          <option>Mr</option> 
          <option>Mrs</option> 
          <option>Dr</option> 
          <option>Miss</option> 
         </select> 
        </div> 
       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.FirstName, "First Name", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-sm-6"> 

         @Html.LabelFor(model => model.LastName, "Last Name", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
       </div> 

       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.Address, "Address", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.Suburb, "Suburb", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.Suburb, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.HomePhone, "Home Phone", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.HomePhone, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 


        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.Mobile, "Mobile", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.Email, "Email", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 
      </div> 

      <div class="panel-heading tabbable" tabindex="0"><h1>Emergency Details</h1></div> 

      <div class="panel-body"> 

       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.EmergencyContactName, "Name", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.EmergencyContactName, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.EmergencyContactRelation, "Relation", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.EmergencyContactRelation, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.EmergencyContactPhone, "Phone", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.EmergencyContactPhone, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 


        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.EmergencyContactEmail, "Email", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.EmergencyContactEmail, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 


      </div> 

      <div class="panel-heading tabbable" tabindex="0"><h1>Driving Licence Details</h1></div> 

      <div class="panel-body"> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.DrivingLicenceNo, "Licence No", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.DrivingLicenceNo, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.DrivingLicenceExpiryDate, "Expiry Date", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.DrivingLicenceExpiryDate, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 

       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.DrivingLicenceImage, "Licence Image", htmlAttributes: new { @class = "control-label" }) 
         <input type="file" name="uploadFile" /> 

        </div> 
        <div class="col-sm-6"> 
         @Html.LabelFor(model => model.DateofBirth, "Date of Birth", htmlAttributes: new { @class = "control-label" }) 
         @Html.EditorFor(model => model.DateofBirth, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 



       </div> 

      </div> 
      <input type="submit" class="btn btn-primary form-control" value="Submit" /> 
     </div> 
    </div> 

    <div class="col-md-4"> 
     <div class="panel panel-default"> 
      <div class="panel-heading">Notes</div> 
      <div class="panel-body"> 

       <textarea rows="10" cols="15" class="form-control" id="Notes"></textarea> 

      </div> 
     </div> 
    </div> 


    <div class="col-md-4"> 
     <div class="panel panel-default"> 
      <div class="panel-heading">Invoice</div> 
      <div class="panel-body"> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 

         <label for="Paid">Paid</label> 
         <input class="form-control" type="text" id="Paid" placeholder="Amount Paid"> 
        </div> 
       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 

         <label for="Balance">Balance</label> 
         <input class="form-control" type="text" id="Balance" placeholder="Balance"> 
        </div> 
       </div> 
       <div class="form-group row"> 
        <div class="col-sm-6"> 

         <label for="Total">Total</label> 
         <input class="form-control" type="text" id="Total" placeholder="Total Amount"> 
        </div> 
       </div> 
       <a href="invoice.html" target="_blank">Print Invoice</a> 
      </div> 
     </div> 
    </div> 

</div> 

}

ответ

3

Вы fields не properties, вам необходимо изменить определение вида модели, чтобы иметь такие свойства, как:

public class RegisterViewModel 
{ 
    public string Title { get; set;} 
    public string FirstName { get; set;} 
    //............ 
    //............ 
} 
+1

спасибо. не могу поверить, что я пропустил это. – Baahubali

2

модели по умолчанию вяжущего в ASP .NET MVC привязывается только к общедоступным свойствам, однако в настоящее время вы используете поля, которые не будут связаны.

Вам просто нужно, чтобы украсить их необходимыми геттеры/сеттеры, как видно ниже, чтобы превратить их в свойства:

public class RegisterViewModel 
{ 
    public string Title { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    // Others omitted for brevity 
} 
Смежные вопросы