剃刀形成asp.net mvc

cod*_*eaK 6 c# razor asp.net-mvc-4

<% using (Html.BeginForm("DisplayCustomer","Customer",FormMethod.Post))
{ %>
Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
Enter customer code :- <%= Html.TextBox("CustomerCode",Model) %><br />
Enter customer Amount :- <%= Html.TextBox("Amount",Model) %><br />
<input type="submit" value="Submit customer data" />
<%} %>
Run Code Online (Sandbox Code Playgroud)

使用剃刀

    @using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) { 
    @Html.ValidationSummary(true);

    @:Enter customer id :- @Html.TextBox("Id",Model)
    @:Enter customer code :-@Html.TextBox("Code",Model)
    @:Enter customer Amount :-@Html.TextBox("Amount",Model)
    <input type="submit" value="Enter the value" />

} 
Run Code Online (Sandbox Code Playgroud)

编译器错误消息:CS1973: 'System.Web.Mvc.HtmlHelper'没有名为'TextBox'的适用方法,但似乎有一个名称的扩展方法.无法动态分派扩展方法.考虑转换动态参数或调用扩展方法而不使用扩展方法语法.

@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) { 
    @Html.ValidationSummary(true);

    @:Enter customer id :- <input type="text" name="Id" />
    @:Enter customer code :-<input type="text" name="Code" />
    @:Enter customer Amount :-<input type="text" name="Amount" />
    <input type="submit" value="Enter the value" />

}
Run Code Online (Sandbox Code Playgroud)

这工作正常

我做错了什么.我正在尝试使用剃刀相当于第一部分给出的代码.第二部分是剃刀代码尝试,第三部分只使用表单时工作.我也想知道嵌套是否是原因错误正在发生

编辑:-

  @:Enter customer id :- @Html.TextBox("Id")
  @:Enter customer code :-@Html.TextBox("Code")
  @:Enter customer Amount :-@Html.TextBox("Amount")
Run Code Online (Sandbox Code Playgroud)

如果我不使用模型,那么一切都很完美

调节器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class DisplayCustomerController : Controller
    {
        //
        // GET: /DisplayCustomer/
        [HttpPost]
        public ViewResult DisplayResult()
        {
            Customer objCustomer = new Customer();
            objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString());  //101; 
            objCustomer.Amount =Convert.ToDouble(Request.Form["Amount"].ToString());// 80.00;
            objCustomer.Code = Request.Form["Code"].ToString();//"IE100"; 

            return View("DisplayResult", objCustomer);
        }
        public ActionResult ShowForm() 
        {

            return View("ShowForm");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class Customer
    {
        //private string Code;
      //  private int Id;
        //private double Amount;
        public String Code
        {
            get;
            set;
        }
        public int Id
        {
            get;
            set;
        }
        public double Amount
        {
            get;
            set;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

查看(ShowForm)

@{
    ViewBag.Title = "ShowForm";
}

<h2>ShowForm</h2>

@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post)) { 
    @Html.ValidationSummary(true);

    @:Enter customer id :- @Html.TextBox("Id")
    @:Enter customer code :-@Html.TextBox("Code")
    @:Enter customer Amount :-@Html.TextBox("Amount")
    <input type="submit" value="Submit" />

}
Run Code Online (Sandbox Code Playgroud)

小智 10

发生错误是因为您尚未在视图中声明模型.将视图修改为

@model MvcApplication1.Models.Customer // add this
@{
    ViewBag.Title = "ShowForm";
}
....
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用,也会发生错误 @model dynamic

不过,我建议您对代码进行一些更改.首先,始终使用视图模型,尤其是在编辑时(请参阅MVC中的什么是ViewModel?).最好使用这些***For()方法来生成控件,并且当前使用@Html.TextBox("Id", Model)意味着每个文本框都会显示"MvcApplication1.Models.Customer",而不是属性的值.此外,还用于LabelFor()生成与控件关联的标签,并包含ValidationMesageFor()用于验证

public class CustomerVM
{
    [Display(Name = "Customer Id")]
    [Required(ErrorMesage = "Please enter the customer id")]
    public int? Id { get; set; }
    [Display(Name = "Customer Code")]
    [Required(ErrorMesage = "Please enter the customer code")]
    public string Code { get; set; }
    [Display(Name = "Customer Amount")]
    [Required(ErrorMesage = "Please enter the customer amount")]
    public double? Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并且视图将是

@model yourViewModelAssembly.CustomerVM
....

@using (Html.BeginForm("DisplayResult", "DisplayCustomer", FormMethod.Post))
{
    @Html.AntiForgeryToken();
    @Html.ValidationSummary(true);
    @Html.LabelFor(m => m.Id)
    @Html.TextBoxFor(m => m.Id)
    @Html.ValidationMessageFor(m => m.Id)
    @Html.LabelFor(m => m.Code)
    @Html.TextBoxFor(m => m.Code)
    @Html.ValidationMessageFor(m => m.Code)
    ....
}
Run Code Online (Sandbox Code Playgroud)

附注:如果Id属性是PK,则不应将其作为可编辑控件包含在视图中.

它还不清楚你为什么要提交不同的方法,并且在任何情况下你的POST方法都不对数据做任何事情(例如保存它).它只是返回相同的数据,但返回到不同的视图.通常你的方法是

public ActionResult Create()
{
    var model = new CustomerVM();
    return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CustomerVM model)
{
    if (!ModelState.IsValid)
    {
        return View(model)
    }
    var customer = new Customer
    {
        Code = model.Code,
        Amount = model.Amount
    }
    .... // save the Customer and redirect
}
Run Code Online (Sandbox Code Playgroud)