2017-05-30 86 views
0

我的应用程序中有一个视图,模型和控制器。我需要的是将文本框的值传递给视图并显示在结果页面上。VIew未访问模型值

Index.cshtml 


    @model MvcApplication5.Models.Employee 
    @{ 
    ViewBag.Title = "Dashboard"; 
    } 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 

    <h2>Dashboard</h2> 
    <div> 
    <h1>Employee </h1> 

    </div> 
    @using (Html.BeginForm("DisplayForm", "Dashboard", FormMethod.Post)) 
     { 
     @Html.EditorFor(model =>model.username) 
     @Html.TextBoxFor(model => model.Company) 
     <button type="submit" id="ajax_method">submit </button> 

     } 

控制器

public ActionResult DisplayForm(Employee model) 
     { 
     var employeeName = model.username; 
     var company = model.Company; 
     return View("Validateresult"); 
     } 
Model 
 public class Employee 
     {   
      public string username { get; set; } 
      public string Company { get; set; } 

     } 

和validateresult.cshtml

@model MvcApplication5.Models.Employee  
    <h2>Validateresult</h2> 
    <div>  
     <div>username: 
     @Html.DisplayFor(model => model.username) 


     </div> 
    </div> 

这似乎是一个愚蠢的问题,但即时通讯无法找到它为什么不显示我的文本框中输入用户名中的原因。该值可在控制器中访问,但不能在视图中访问。

+2

'return View(“Validateresult”);'to'return View(“Validateresult”,model);' – User3250

回答

3

您没有将模型传递给视图。 在控制器中,将return View("Validateresult");更改为return View("Validateresult", model);