如何使用MVC在TextBox中获取和设置值? [英] How do you get and set the value in TextBox using MVC?

查看:98
本文介绍了如何使用MVC在TextBox中获取和设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用MVC在TextBox中获取和设置值?

How do you get and set the value in TextBox using MVC?

推荐答案

在MVC中,我们使用View Model将值绑定到视图,就像我一样使用下面的Razor视图引擎语法: -

In MVC, we use View Model to bind the values to the view like I am using Razor view engine syntax below:-
@Html.TextBoxFor(m=>m.ValueForTextBox) //Binded the value to the textbox model and passed
@Html.ValidationFor(m=>m.ValueForTextBox) //If any validation is applied

//On the Server side:-
public ActionResult GetValueForTextBox()
{
    var viewModel = new ValueForTextBoxViewModel
     {
         ValueForTextbox = "Fetch and assign the value here"      
     }
    return View(viewModel);
}

//ViewModel goes below:
public class ValueForTextBoxViewModel
{
    [Required("Required Field")] //Validation Message
    public string ValueForTextbox {get; set;}
}





希望这会有所帮助

谢谢

:)



Hope this helps anyway
Thanks
:)


这篇关于如何使用MVC在TextBox中获取和设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆