开发者

ASP.NET MVC Posted date field comes in as 1/1/0001

Just started working with .NET and MVC(1). I'm having a problem wherein in my add action the entered date for some reason ends up as 1/1/0001 instead of what is entered thus causing a date overflow.

In my model, this field ("Added") is is of type datetime and does not allow nulls.

In my controller I have:

public ActionResult Add()
    {
        Instance instance = new Instance()
        {
            Added = DateTime.Now,
            Active = true
        };

        return View(instance);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(Instance instance)
    {

        if (ModelState.IsValid)
        {
            try
            {
                System.Diagnostics.Trace.Write("test");
                instanceRepository.Add(instance);
                instanceRepository.Save();

                return RedirectToAction("Details", new { id = instance.InstanceID });
            }
            catch
            {
                ModelState.AddRuleViolations(instance.GetRuleViolations());
            }
        }
        return View(instance);
    }

And in my view I have:

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Added) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Added,String.Format("{0:g}",Model.Added))%>
            <%= Html.ValidationMessageFor(model => model.Added) %>

When I first go to Instances/Add the default value is set correctly, however as soon as I submit the date turns into 1/1/0001 (from my understanding this indicates that it was either null or in an unrecognizable format)

When I debug and palce a watch on Request.Form I see the date coming in encoded ie Request.Form {Added=4%2f9%2f2010+8%3a24%3a39+AM} - is this an issue?

I know its probably not enough information to make 开发者_Python百科a conlusive determination on why this is failing, but if someone could provide some good debugging tips on how to determine where the submitted date is getting munged I'd really appreciate it.


The issue is that the String.Format("{0:g}", Model.ExpirationDate) bit above is the TemplateName parameter of the TextBoxFor method. The view engine won't be able to find a template which matches the date.

What you need to do is decorate your model

// inside your Instance model
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g}")]
[DisplayName("Added")]
public DateTime Added { get; set; }

and then use the following in your view

<%= Html.EditorFor(model => model.DateRequired)%>

And I got this solution from here.


It looks like it is a localization problem IMO.

See this post


Thanks for all the answers... The mistake turned out to be in my partial model class:

namespace MSSQL_Manager.Models
{
[Bind(Include="xxxxxx,xxxxx,xxxx,xxxx")]
public partial class Instance
{
    public bool IsValid
    {
        get { return (GetRuleViolations().Count() == 0); }
    }

I didn't have the 'Added' field in the [Bind(Include="xxxx")] attribute.

Need to read up more on attributes - thanks everyone for their time

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜