简体   繁体   English

简单的C#和MVC项目

[英]Simple C# and MVC project

I just started learning C# and tried to create a simple project. 我刚刚开始学习C#,并试图创建一个简单的项目。 First I Created a textbox and when someone enters a name in it, the name should show up in the next page but not sure what I'm doing wrong. 首先,我创建了一个文本框,当有人在其中输入名称时,该名称应显示在下一页,但不确定我在做什么错。 Textbox and submit button are showing up. 出现文本框和提交按钮。 After typing the name in the textbox and clicking on submit it doesn't show up in the next page. 在文本框中输入名称并单击“提交”后,该名称不会显示在下一页中。 PLEASE HELP 请帮忙

Home Controller: 家庭控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Newproject.Models;

namespace Newproject.Controllers
{
public class HomeController : Controller
{
    public ActionResult Index()

    {
        return View();
    }

    public ActionResult Welcome(FormCollection form, string CustNo)

    {
       Session["SearchString"] = form["CustNo"];
         ViewBag.Name = Session["SearchString"];
          return View();
    } 
  }
 }

Index.cshtml Index.cshtml

@{
ViewBag.Title = "Access TextBox and TextboxFor Value From View To Controller In Asp.net MVC";

}

    <br />
    @Html.Label("Name")
   <br />
    @using (Html.BeginForm("Welcome","Home",FormMethod.Post))
    { 
    @Html.TextBox("SearchString", "", new { @class = "CustNo" });
        <br/>
    <input id="btnSubmit" name="btnSubmit" placeholder="test" type="submit"  onclick = "location.href='@Url.Action("Welcome", "Home", new {})'" />
        <br/>

    }

Welcome.cshtml Welcome.cshtml

@{
ViewBag.Title = "Welcome";
string CustNo = ViewBag.Name;
 }
   <p> @CustNo</p>

Your input' field's name attribute value is SearchString . 输入字段的名称属性值为SearchString So you should be looking for that, not CustNo . 因此,您应该寻找的不是CustNo CustNo is the css class name of your input field, You cannot read the value of the input field using the css class name ( unless you are doing some jquery stuff on the form submit ) CustNo是您输入字段的css类名,您不能使用css类名读取输入字段的值( 除非您在表单commit上进行了一些jquery的工作

public ActionResult Welcome(string SearchString)
{  
    ViewBag.Name = SearchString;
    return View();
} 

Also make sure you are not doing a redirect using javascript when submit button is clicked, but doing a normal form submit. 另外,请确保在单击“提交”按钮时没有使用javascript进行重定向,而是进行普通表单提交。

Simply remove the onclick handler. 只需删除onclick处理程序。 Your button is inside a form and when you click the submit button the form will be submitted (with the form data). 您的按钮位于表单内部,当您单击“提交”按钮时,将提交表单(包含表单数据)。

@using (Html.BeginForm("Welcome","Home",FormMethod.Post))
{
   @Html.TextBox("SearchString", "", new { @class = "CustNo" });
   <input id="btnSubmit" name="btnSubmit" placeholder="test" type="submit" />
}

Modify your home controller 修改您的家庭控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Welcome(FormCollection form)
    {
        ViewBag.Name = form["SearchString"];
        return View();
    } 
}

Replace your Html.BeginForm code in Index.cshtml 替换Index.cshtml中的Html.BeginForm代码

@using (Html.BeginForm("Welcome", "Home", FormMethod.Post))
{ 
  @Html.TextBox("SearchString")
  <br />
  <input id="btnSubmit" name="btnSubmit" type="submit" />
  <br />
}

在此处输入图片说明

Now result: 现在结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM