Question

My controller:

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

My model:

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

My AddEstablishment View:

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

Result:

enter image description here

My textbox is prepopulating with the Guid default value

00000000-0000-0000-0000-000000000000

How can i avoid it?

Please note there are obvious reasons why I'm passing an instance of the model to the view.

Was it helpful?

Solution

Shouldn't this work?

 public Guid? EstablishmentId { get; set; }

Just ensure that the value is null when you initially display your view.

OTHER TIPS

You don't need to pass the model into the view, it will work if you don't pass it

If you really have to, do this:

private string _establishmentIdAsString = string.Empty;
[Display(Name = "Establishment ID")]
public string EstablishmentIdAsString
{
    get
    {
        return _establishmentIdAsString;
    }
    set
    {
        _establishmentIdAsString = value;
        EstablishmentId = new Guid(value);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top