Razor表单ASP.NET MVC

6
<% 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" />
<%} %>

使用Razor

    @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" />

} 

编译器错误消息: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" />

}

这个正确地运行了。

我做错了什么?我试图使用第一部分给出的代码的razor等效代码。第二部分是razor代码尝试,当只使用表单时第三部分起作用。我还想知道是否嵌套是发生错误的原因。

编辑:

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

如果我不使用模型,一切都可以完美运行。

控制器

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");
        }

    }
}

Model

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;
        }

    }
}

查看(展示表单)

@{
    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" />

}

你正在将整个模型传递给每个文本框。应该是 Model.Code 或者你需要显示的任何属性名称。我不认为这是错误的原因,但无论如何都应该修复。 - user5886152
1
你是如何声明你的模型的?使用了 @model dynamic 吗? - user247702
@SachinDivakar,您需要在视图顶部声明模型 - 假设您的模型是class MyModel,那么在视图中添加@model yourAssembly.MyModel - user3559349
@SachinDivakar 你的Razor中的Model是什么,你是从ViewBag中获取它吗? - Saket Choubey
@StephenMuecke 我已经提供了完整的代码。 - codefreaK
显示剩余11条评论
1个回答

11

您的错误发生是因为您没有在视图中声明模型。 修改视图以

@model MvcApplication1.Models.Customer // add this
@{
    ViewBag.Title = "ShowForm";
}
....

请注意,如果您使用@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; }
}

视图将会是:

@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)
    ....
}

顺带一提,如果Id属性是主键,那么它不应作为可编辑控件包含在视图中。

同时也不清楚为什么要提交给另一个方法,无论如何,您的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
}

这是关于另一个问题的内容。我正在尝试使用WebMethod进行jQuery Ajax表单提交,但现在我遇到了一个情况,表单还包含文件上传部分,所以我正在尝试将提交的数据以JSON格式发送到WebMethod。但现在我被迫为每个文本框使用1个参数变量,而且我无法检索已上传的文件。我已经了解到表单可以序列化,但如何将表单作为参数接收并反序列化它呢? - codefreaK
你需要提出一个新问题,并附上相关的细节和代码(你可以在这里添加链接,但我需要几个小时才能查看)。 - user3559349
这是我的问题,感谢:http://stackoverflow.com/questions/38754763/ajax-jquery-form-post-using-web-methods-c-sharp-asp-net - codefreaK
@SachinDivakar,这是WebForms代码(我已经很久没有使用过了),但是请查看此答案以序列化表单,包括文件输入,并使用ajax提交它:https://dev59.com/0l4b5IYBdhLWcg3wVwFi#29293681 - user3559349
Web Methods是一种过时的技术吗?大家都转向MVC了吗? - codefreaK
让我们在聊天中继续这个讨论 - user3559349

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接