Skip to main content
 首页 » 编程设计

c#之获取和处理 MVC 中输入的数据之简单

2024年02月23日14傻小

我对我的 MVC 学习曲线有一些疑问

目标:

我想获取 txtMHM 输入的文本 在推送 btnStat 并通过标签或跨度或...显示输入的文本之后

请您参考如下方法:

您应该使用的 View 模型(简化):

public class YourViewModel 
{ 
    public string TextEntered { get ; set ; } 
} 

View :

@model YourViewModel 
 
@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(m => m.TextEntered) 
    <br /> 
    <input id="btnStat" type="submit" value="MHM" /> 
} 
 
@Html.LabelFor(m => m.TextEntered) 

Controller Action 方法:

[HttpPost] 
public ActionResult ChangeLabelText(YourViewModel yourViewModel) 
{ 
    return View(yourViewModel); 
} 



你修改后的代码

index.cshtml

@model MVCTest1.Models.EnteredTextModel  
 
 
@using (Html.BeginForm())  
{  
    @Html.TextBoxFor(m => m.TextEntered)  
    <br />  
    <input id="btnStat" type="submit" value="MHM" />  
}  
 
@Html.LabelFor(m => m.TextEntered) 

模型

namespace MVCTest1.Models  
{  
 
    public class EnteredTextModel  
    {  
        public string TextEntered { get; set; }  
 
    }  
}  

家庭 Controller

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
using MVCTest1.Models ;  
 
namespace MVCTest1.Controllers  
{  
    public class HomeController : Controller  
    {  
        public ActionResult Index()  
        {  
            ViewBag.Message = "Welcome to ASP.NET MVC!";  
 
            return View();  
        }  
 
        [HttpPost] 
        public ActionResult Index(EnteredTextModel theModel)  
        {  
            return View(theModel);  
        }  
 
        public ActionResult About()  
        {  
            return View();  
        }  
 
    }  
}