如何使用MVC 4捕获用户输入并存储在变量中 [英] How to capture User Input and store in a variable using MVC 4

查看:58
本文介绍了如何使用MVC 4捕获用户输入并存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello House,

请我是MVC 4的新用户,我希望能够捕获用户在表单上输入的内容并存储在变量中。



此外,我希望能够在视图上显示主键字段,以便用户可以手动输入。提前谢谢。

解决方案

将Id字段也放在下面的标记中也删除了数据库中id字段的autoincrement属性。模型中的Id字段也是如此。

 <   div     class   = 编辑标签 >  
@ Html.LabelFor(model => model.Id)
< / div >
< span class =code-keyword>< div class = editor-field >
@ Html.EditorFor(model => model.Id)
@ Html.ValidationMessageFor(model => model.Id)
< / div >





希望这有助于


请完成我分享的步骤。这是一个用于将人员详细信息发布到服务器的示例应用程序。



步骤1:通过右键单击控制器来创建控制器,如下所示

< pre lang =cs> public class PersonController:Controller
{
< span class =code-comment> //
// < span class =code-comment> GET:/ Person /

public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult创建(个人)
{

尝试
{
// TODO:在此处添加插入逻辑
string name = person.Name;
return RedirectToAction( Index);
}
catch
{
return 查看( );
}
}

}



步骤2:在模型文件夹中创建一个模型,如下所示

  public   class  Person 
{
[Key]
public int Id {获得; set ; }
public string 名称{ get ; set ; }
public int 年龄{ get ; set ; }
}





步骤3:通过右键创建视图单击我在控制器中提到的索引操作并将其放入通过删除现有标记在该视图中标记

 @ model MvcApplication1.Models.Person 
@ {
Layout = ;
}



 <   html  >  
< head >
< meta 名称 = viewport content = width = device-width / >
< title > 索引< / title >
< < span class =code-leadattribute> / head >
< body >

@using(Html.BeginForm(Create) ,人)){
@ Html.ValidationSummary(true)

< fieldset >
< 图例 > < / legend >
< div class = editor-label >
@ Html.LabelFor(model => model.Id)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.Id)
@ Html.ValidationMessageFor(model => model.Id)
< / div >
< div class = editor-label & gt;
@ Html.LabelFor(model => model.Name)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.Name)
@ Html.ValidationMessageFor(model => model.Name)
< / div >

< div class = 编辑标签 >
@ Html.LabelFor(model => model.Age)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.Age)
@ Html.ValidationMessageFor(model => model.Age)
< / div >

< p >
< 输入 类型 = 提交 < span class =code-attribute> value = 创建 / > ;
< / p >
< / fieldset >
}
< / body >
< / html >





步骤4:避免Id的Autoincrement属性数据库中的字段


替换现有视图,如下所示。在YourControllerName的位置更改controllerName

 @ model AccountInfoModel 

@ {
Layout = null;
}



< html >
< head >
< meta 名称 = < span class =code-keyword> viewport content = width = device-width / >
< title > 索引< / title >
< / head >
< 正文 >

@using(Html.BeginForm(创建, YourControllerName )){
@ Html.ValidationSummary(true)

< fieldset >
< 图例 > 创建< / legend >

< div class = editor-label >
@ Html.LabelFor(model => model.UserName)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.UserName)
@ Html.ValidationMessageFor(model => model.UserName)
< / div >

< div class = 编辑标签 & gt;
@ Html.LabelFor(model => model.Password)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.Password)
@ Html.ValidationMessageFor(model => model.Password)
< / div >

< div class = 编辑标签 & gt;
@ Html.LabelFor(model => model.ConfirmPassword)
< / div >
< div class = editor-field >
@ Html.EditorFor(model => model.ConfirmPassword)
@ Html.ValidationMessageFor(model => model.ConfirmPassword)
< / div >


< p < span class =code-keyword>>
< 输入 type = submit value = 创建 / >
< / p >
< / fieldset < span class =code-keyword>>
}
< / body >
< / html >


Hello House,
Please i am new to MVC 4, i want to be able to capture what user enters on a form and store in a variable.

Also, i want to be able to display primary key field on view so that users can enter it manually. Thanks in advance.

解决方案

Put Id field also in the markup like below also remove autoincrement property of the id field in the database.Put Id field in the model also.

<div class="editor-label">
                @Html.LabelFor(model => model.Id)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>



Hope this helps


Please go through the steps i have shared. This a sample application for posting the person details to the server.

Step1: Create a Controller by Right clicking the Controller like below

public class PersonController : Controller
   {
       //
       // GET: /Person/

       public ActionResult Index()
       {
           return View();
       }

       [HttpPost]
       public ActionResult Create(Person person)
       {

           try
           {
               // TODO: Add insert logic here
               string name = person.Name;
               return RedirectToAction("Index");
           }
           catch
           {
               return View();
           }
       }

   }


Step2 : Create a Model in the Model Folder like below

public class Person
   {
       [Key]
       public int Id { get; set; }
       public string Name { get; set; }
       public int Age { get; set; }
   }



Step3:Create a View by Right Clicking the Index Action that i have mentioned in the controller and put the mark up in that view by removing the existing markups

@model MvcApplication1.Models.Person
@{
    Layout = null;
}


<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    
    @using (Html.BeginForm("Create","Person")) {
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>Person</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Id)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Age)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Age)
                @Html.ValidationMessageFor(model => model.Age)
            </div>
    
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
</body>
</html>


.
Step4:Avoid the Autoincrement property of Id field from the Database


Replace the existing view like below. Change the controllerName in the place of YourControllerName

@model AccountInfoModel

@{
    Layout = null;
}



<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    
    @using (Html.BeginForm("Create","YourControllerName")) {
        @Html.ValidationSummary(true)
    
       <fieldset>
        <legend>Create</legend>
        
        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>

        
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
    }
</body>
</html>


这篇关于如何使用MVC 4捕获用户输入并存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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