2017-01-16 30 views
-1

我是新來的asp.net mvc。我已經爲其他兩個字段定製了mvc標識。現在,我會讓用戶更新電子郵件,電話號碼等用戶信息。當我點擊編輯時,我的文本框中沒有任何內容。文本框字段的HTMLEdit爲空。爲更新的東西,我不得不添加電子郵件,然後如果我點擊提交按鈕,值得到數據庫中的更改。不顯示MVC的編輯文本框中的數據

這裏的問題是,當我點擊編輯按鈕時,在用其他電子郵件ID進行任何更改之前,不會顯示數據庫中的用戶數據。

這裏是我的代碼

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> UpdateUser(ProfileViewModels model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

      //get current user and update 
      var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); 
     if (user == null) 
     { 
      // Don't reveal that the user does not exist 
      return RedirectToAction("Login", "Account"); 
     } 
     user.Email = model.Email; 
     user.UserName = model.Email; 
     //user.LastName = model.LastName; 

     var updateResult = await UserManager.UpdateAsync(user); 
     if (updateResult.Succeeded) 
     { 
      return RedirectToAction("Index", "Home"); 
     } 
     AddErrors(updateResult); 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

CHTML代碼:

@using (Html.BeginForm("UpdateUser", "Manage", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>ProfileViewModels</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

幫助,將不勝感激。

+0

你需要顯示GET方法 - 你如何填充和模型傳遞給視圖 –

回答

0

之前告訴你,你需要創建一個模型,並填充它的觀點。否則,視圖沒有使用的值。因此,像:

public ActionResult UpdateUser() 
{ 
    var userModel = new UserModel(); 
    userModel.Email = 'Some value from the database however you are getting it'; 


    return View(userModel); 
} 
0

您是否嘗試過使用@ Html.TextBoxFor而不是@ Html.EditorFor?你有電子郵件地址的自定義編輯器設置嗎?

+0

是我試過了。仍然沒有顯示數據庫中的值。 – user7090664

+0

您發佈了回傳代碼,從模型中獲取值並將其放入數據庫中。你可以發佈控制器方法,從數據庫中檢索值並填充模型? – MikeS

+0

public ActionResult UpdateUser() { return View(); } – user7090664

0

你可以在

return View(model); 

設置一個斷點所以,你可以檢查模型實際上是空的。

我沒有看到你如何得到模型實例。 您可以添加另一個GET操作來顯示數據庫中的初始值,只接受一個I​​d字段。你的編輯按鈕可以改爲調用這個動作:

public async Task<ActionResult> UpdateUser(string id) 
{ 
    var model = await UserManager.FindByIdAsync(id); 
    if (model == null) 
    { 
     return HttpNotFound(); 
    } 
    return View(model); 
} 

編輯按鈕:

@Html.ActionLink("Edit", "UpdateUser", new { id=Model.YourUserIdField })