剃刀形式asp.net mvc [英] Razor form asp.net mvc

查看:75
本文介绍了剃刀形式asp.net mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


使用剃刀


Using 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'的适用方法,但似乎具有该名称的扩展方法.扩展方法不能动态调度.考虑强制转换动态参数或在不使用扩展方法语法的情况下调用扩展方法.

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

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

}

这正常工作

我在做什么错.我试图使用与第一部分中给出的代码等效的剃刀.第二部分是剃刀代码尝试,当仅使用形式时进行第三次工作.我还想知道是否嵌套错误发生的原因

What am I doing wrong .I am trying to use the razor equivalent of the code given in first section.The second part is the razor code attempt and third works when only form is used .I also want to know if nest is the reason the error is happening

-

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

如果我不使用模型,那么一切都会很好

If I do not use model then everything works perfectly

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

    }
}

型号

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

    }
}

View(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" />

}

推荐答案

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

Your error occurs because you have not declared the model in the view. Modify the view to

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

请注意,如果您使用@model dynamic

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

I would however suggest a number of changes to your code. Firstly, always using a view model, especially when editing (refer What is ViewModel in MVC?). It is also preferable to use the ***For() methods to generate you controls, and you curret use of @Html.TextBox("Id", Model) would mean the each textbox would display "MvcApplication1.Models.Customer", not the value of the property. In addition, use LabelFor() to generate labels associated with your controls, and include ValidationMesageFor() for validation

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属性是PK,则不应将其作为可编辑控件包含在视图中.

Side note: If the Id property is the PK, then that should not be included in the view as an editable control.

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

Its also unclear why you are submitting to a different method, and in any case your POST method does not do anything with the data (e.g. saving it). It just returns the same data back again, but to a different view. Typically your methods would be

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
}

这篇关于剃刀形式asp.net mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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