English 中文(简体)
ASP。NET MVC3:表单有问题,没有显示我的字段值
原标题:ASP.NET MVC3: Problems with forms, not showing my field values

为了验证,我决定尝试使用@Html。TextBoxFor,因此验证既快捷又简单。但是,没有设置值的选项,我假设它是自动完成的,但它不是自动设置的。

控制器:

        MyLibrary.MyProspect prospect = MyLib.GetProspect(ID);
        return View(prospect);

视图:

@model MyLibrary.MyProspect

@using (Html.BeginForm("Update", "Prospects"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Edit</legend>

        @Html.LabelFor(model => model.Name)
        @Html.TextBoxFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)

我对Prospect的库定义:

public class MyProspect
{
    public String Name { get; set; }
}

我的猜测是,在任何时候我都不会将模型设置为真正的潜在客户。但我没有遇到过这样的例子,我想我会像现在这样忽略它吗?

SOLVED This is working code, Id just mixed and matched my controller action to the wrong view.

最佳回答

要使客户端验证工作,您需要做以下几件事:

  1. Decorate some fields in your model using the appropriate DataAnnotations
  2. Ensure you have the ClientValidationEnabled & UnobtrusiveJavaScriptEnabled options set to true. This can be done directly in the <appSettings>.
  3. Include the appropriate libraries in the page i.e. jquery.validate.min.js and jquery.validate.unobtrusive.min.js

此外,如果您没有特殊的视图要求,您可以将代码简化为:

@Html.EditorForModel()
问题回答

你写的代码看起来是正确的,我猜你的问题在MyLib中。GetProspect(ID)未设置“名称”字段的值。

@model MyLibrary。MyProspect此行将您的模型设置为MyProspect。

您的MyProspect上有DataAnnotations吗。名称属性?

也许可以添加您对MyProspect的定义。名字对你的问题也会有所帮助。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签