c# – Razor形式asp.net mvc
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Razor形式asp.net mvc,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4602字,纯文字阅读大概需要7分钟。
内容图文
![c# – Razor形式asp.net mvc](/upload/InfoBanner/zyjiaocheng/805/006be85aab1d4b23a792909f58b3c0d1.jpg)
<% 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 (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" />
}
这工作正常
我做错了什么.我正在尝试使用剃刀相当于第一部分给出的代码.第二部分是剃刀代码尝试,第三部分只使用表单时工作.我也想知道嵌套是否是原因错误正在发生
编辑:-
@: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");
}
}
}
模型
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;
}
}
}
查看(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" />
}
解决方法:
发生错误是因为您尚未在视图中声明模型.将视图修改为
@model MvcApplication1.Models.Customer // add this
@{
ViewBag.Title = "ShowForm";
}
....
请注意,如果使用@model dynamic,也会出现错误
不过,我建议您对代码进行一些更改.首先,始终使用视图模型,尤其是在编辑时(参见What is ViewModel in MVC?).最好使用*** 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属性是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
}
内容总结
以上是互联网集市为您收集整理的c# – Razor形式asp.net mvc全部内容,希望文章能够帮你解决c# – Razor形式asp.net mvc所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。