2011-01-25 77 views
0

從控件的視圖中獲取用戶輸入的最佳方式是什麼?我的意思是特定的輸入不喜歡像「對象者」或「int值」,以及如何刷新一定的時間間隔ASP NET MVC 3.0 GET USER INPUT

回答

0

比方說,如果你的看法是強類型w ^第i個 「人」 類:

public class Person 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Age { get; set; } 
} 

那麼你的視圖中:

@model MyMVCApp.Person 
@using(Html.BeginForm()) 
{ 
    @Html.EditorForModel() 
    // or Html.TextBoxFor(m => m.FirstName) .. and do this for all of the properties. 
    <input type="submit" value="Submit" /> 
} 

然後你必須將處理表單的動作:

[HttpPost] 
public ActionResult Edit(Person model) 
{ 
    // do stuff with model here. 
} 

MVC使用什麼稱爲ModelBinders將該表單集合映射到模型。

要回答你的第二個問題,你可以刷新頁面下面的JavaScript:

<script type="text/javascript"> 
// Reload after 1 minute. 
setTimeout(function() 
{ 
    window.location.reload(); 
}, 60000); 
</script> 
5

頁面通過編寫視圖模型「的FormCollection」成才:

public class UserViewModel 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

控制器:

public class UsersController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new UserViewModel()); 
    } 

    [HttpPost] 
    public ActionResult Index(UserViewModel model) 
    { 
     // Here the default model binder will automatically 
     // instantiate the UserViewModel filled with the values 
     // coming from the form POST 
     return View(model); 
    } 

} 

查看:

@model AppName.Models.UserViewModel 
@using (Html.BeginForm()) 
{ 
    <div> 
     @Html.LabelFor(x => x.FirstName) 
     @Html.TextBoxFor(x => x.FirstName) 
    </div> 
    <div> 
     @Html.LabelFor(x => x.LastName) 
     @Html.TextBoxFor(x => x.LastName) 
    </div> 

    <input type="submit" value="OK" /> 
} 
+0

+1打字比我快多了:) – TheCloudlessSky 2011-01-25 13:18:30