2

我的控制器:

 [HttpGet]
 public ActionResult AddEstablishment()
 {     
    return View("AddEstablishment",new EstablishmentModel());
 }

我的模型:

public class EstablishmentModel
{
    [Display(Name = "Establishment ID")]
    public Guid EstablishmentId { get; set; }
    .............

我的 AddEstablishment 视图:

 @Html.TextBoxFor(x => x.EstablishmentId, new { @id = "inputEstGuid", @class = "input input-xlarge", @placeholder = "5C3B1CBC-2574-4E2A-A9FA-A8CA0041AB86" })

结果:

在此处输入图像描述

我的文本框预填充了 Guid 默认值

00000000-0000-0000-0000-000000000000

我怎样才能避免它?

请注意,我将模型实例传递给视图的原因很明显。

4

2 回答 2

4

这不应该工作吗?

 public Guid? EstablishmentId { get; set; }

只需确保在最初显示视图时该值为 null。

于 2013-02-28T14:38:14.537 回答
1

您不需要将模型传递到视图中,如果您不传递它,它将起作用

如果你真的必须这样做,请执行以下操作:

private string _establishmentIdAsString = string.Empty;
[Display(Name = "Establishment ID")]
public string EstablishmentIdAsString
{
    get
    {
        return _establishmentIdAsString;
    }
    set
    {
        _establishmentIdAsString = value;
        EstablishmentId = new Guid(value);
    }
}
于 2013-02-28T14:39:37.233 回答