2017-12-02 45 views
0

我使用.NET核心2.我已經找到了渲染剃刀視圖的字符串的解決方案。較低的MVC版本,我用它和everthing是好的。 我得到結果/表單到引導模態。當我發佈到控制器(例如UserName空值)時,我無法從ModelState獲得結果到<span asp-validation-for="UserName" class="text-danger"></span>,哪裏有問題? 當我不使用RenderToStringAsync方法和return View()我從ModelState得到<span asp-validation-for="UserName" class="text-danger"></span>的結果。 總是actionContext ModelState值或鍵也是空的。剃刀視圖字符串與ASP.NET Core 2表單輸入驗證

我的服務器端:

public class RazorViewToStringRenderer 
    { 
     private readonly IRazorViewEngine viewEngine; 
     private readonly ITempDataProvider tempDataProvider; 
     private readonly IHttpContextAccessor _httpContext; 

     public RazorViewToStringRenderer(
      IRazorViewEngine viewEngine, 
      ITempDataProvider tempDataProvider, 
      //IServiceProvider serviceProvider, 
      IHttpContextAccessor httpContext) 
     { 
      this.viewEngine = viewEngine; 
      this.tempDataProvider = tempDataProvider; 
      _httpContext = httpContext; 
     } 

     public async Task<string> RenderToStringAsync(string viewName, object model) 
     { 
      var httpContext = _httpContext.HttpContext; 
      var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); 

      using (var sw = new StringWriter()) 
      { 
       var viewResult = viewEngine.FindView(actionContext, viewName, false); 

       if (viewResult.View == null) 
       { 
        throw new ArgumentNullException($"{viewName} does not match any available view"); 
       } 

       var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) 
       { 
        Model = model 
       }; 

       var viewContext = new ViewContext(
         actionContext, 
         viewResult.View, 
         viewDictionary, 
         new TempDataDictionary(actionContext.HttpContext, tempDataProvider), 
         sw, 
         new HtmlHelperOptions() 
        ) 
       { RouteData = httpContext.GetRouteData() }; 


       await viewResult.View.RenderAsync(viewContext); 
       return sw.ToString(); 
      } 
     } 
    } 

客戶端:

@model EditUserViewModel 

     <form asp-controller="Account" asp-action="EditUser" asp-antiforgery="true" data-ajax-success="onSuccess" data-ajax="true" data-ajax-begin="onBegin" method="post" class="form-horizontal" role="form"> 
    .... 
      <div class="form-group"> 
       <div class="col-sm-12"> 
        <label asp-for="UserName" class="col-form-label"></label> 
        <input asp-for="UserName" class="form-control" /> 
        <span asp-validation-for="UserName" class="text-danger"></span> 
       </div> 
      </div> 
    .... 
    </form> 

控制器:

[HttpPost] 
public async Task<IActionResult> EditUser(EditUserViewModel editUserViewModel) 
{ 
    var user = await _userManager.FindByIdAsync(editUserViewModel.Id); 

    if (user != null) 
    { 
     user.UserName = editUserViewModel.UserName; 
     user.FirstName = editUserViewModel.FirstName; 
     user.LastName = editUserViewModel.LastName; 

     var result = await _userManager.UpdateAsync(user); 
     if (ModelState.IsValid) 
     { 

      if (result.Succeeded) 
       return Json("Ok"); 
     } 
     else 
     { 
      return Json(new 
      { 
       ModalClose = false, 
       Data = _renderer.RenderToStringAsync("EditUser", editUserViewModel) 
      }); 

     } 

    } 

    return RedirectToAction("UserManagement", _userManager.Users); 
} 

填寫結果:

function onSuccess(data, status, xhr) 
{ 

    $("#" + modid + " .modal-body").empty(); 
    $("#" + modid + " .modal-body").html(data.data.result); 

} 

視圖模型:

public class EditUserViewModel 
    { 
     public string Id { get; set; } 

     [Display(Name = "User Name")] 
     [Required(ErrorMessage = "UserName required")]  
     public string UserName { get; set; } 

     [Display(Name = "First Name")] 
     [Required(ErrorMessage = "FirstName required")] 
     public string FirstName { get; set; } 
} 

回答

0

我已經加入到Startup.cs:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); 

var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), _contenxt.ActionContext?.ModelState ?? new ModelStateDictionary()) 
{ 
    Model = model 
}; 

似乎它的工作

+1

如果它解決了您的問題,您應該將其標記爲接受的答案。 – CalC