2012-04-22 59 views
0

我想在表中插入一个固定的日期。我怎样才能做到这一点 ?如何在MVC中的表中插入固定值

<div class="editor-label"> 
     Description : 
    </div> 
    <div class="editor-field"> 
     @Html.TextBoxFor(model => model.Description, new { @class = "textboxes" }) 
     @Html.ValidationMessageFor(model => model.Description) 
    </div> 

    <div class="editor-label"> 
     Date : 
    </div> 
    <div class="editor-field"> 
     @Html.TextBoxFor(model => model.Date, new { @class = "textboxes" }) /// I wanna be here a fixed date . 
     @Html.ValidationMessageFor(model => model.Date) 
    </div> 

我的控制器代码是:

public ActionResult Index(tblOrder tblorder) 
     { 
      if (ModelState.IsValid) 
      { 
       db.tblOrders.AddObject(tblorder); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.fxBudjet = new SelectList(db.tblBudjets, "ID", "Budjet", tblorder.fxBudjet); 
      ViewBag.fxServiceType = new SelectList(db.tblServiceTypes, "ID", "Service", tblorder.fxServiceType); 
      ViewBag.fxStartTime = new SelectList(db.tblStartDates, "ID", "StartDate", tblorder.fxStartTime); 
      return View(tblorder); 
     } 

我需要改变我的控制器代码?

+0

可能重复的一些想法。网络MVC](http://stackoverflow.com/questions/10265925/inserting-static-date-or-text-int-database-by-asp-net-mvc) – nemesv 2012-04-22 11:05:37

+0

不完全确定你的意思,但如果你的意思你想只显示我的答案应该工作的价值 – Manatherin 2012-04-22 11:06:43

+0

不,我想通过我的owow插入一个日期n代码,而不是用户..我不想显示任何日期。只是我想用自己的代码注册一个数据库的日期。 – 2012-04-22 11:10:13

回答

2
@Html.DisplayTextFor(model => model.Date, new { @class = "textboxes" }) 

如果需要在后的日期,你可以做

@Html.DisplayTextFor(model => model.Date, new { @class = "textboxes" }) 
@Html.HiddenFor(model => model.Date) 

或POST方法,你可以使用TryUpdateModel和您的更新我排除的日期字段

编辑会在控制器中设置模型的值,例如说你有,你想,如果你想为你可以使用一个hiddenfor或者重新创造它在后

后再次预填充年份值

public ActionResult Create() 
    { 
     return View(new Year() { Value = DateTime.Now.Year }); 
    } 

类一年,如果本场只在POST方法需要一个更好的选择是没有那场创建一个视图模型,然后在POST方法的一些映射逻辑

编辑2:与MVC音乐商店,你真的应该有2种方法get和一个帖子。我会认为这是为了创造。

[HttpGet] 
public ActionResult Create() 
{ 
    ViewBag.fxBudjet = new SelectList(db.tblBudjets, "ID", "Budjet"); 
    ViewBag.fxServiceType = new SelectList(db.tblServiceTypes, "ID", "Service"); 
    ViewBag.fxStartTime = new SelectList(db.tblStartDates, "ID", "StartDate"); 
    return View(new tblOrder() { Date = DateTime.Now }); 
} 

[HttpPost] 
public ActionResult Create(tblOrder tblorder) 
{ 
    if (ModelState.IsValid) 
    { 
     db.tblOrders.AddObject(tblorder); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.fxBudjet = new SelectList(db.tblBudjets, "ID", "Budjet", tblorder.fxBudjet); 
    ViewBag.fxServiceType = new SelectList(db.tblServiceTypes, "ID", "Service", tblorder.fxServiceType); 
    ViewBag.fxStartTime = new SelectList(db.tblStartDates, "ID", "StartDate", tblorder.fxStartTime); 
    return View(tblorder); 
} 

那么在你看来像

@model tblOrder 

@Html.BeginForm() 
{ 
    @Html.DisplayTextFor(model => model.Date, new { @class = "textboxes" }) 
    @Html.HiddenFor(model => model.Date) 
    ...other form stuff 
    <input type="submit" value="Delete" /> 
} 

这应该可以给你如何解决你的代码[通过ASP插入静态日期或文本数据库的

+0

我这样做在Asp.net Webforms通过此代码:scm.parameters.addwithvalues(“@ Date”,DateTime.Now); 。我怎样才能在MVC做这项工作..我自己想要通过我的代码插入日期形式..而不是由用户。 – 2012-04-22 12:10:26

+0

我不需要显示日期..只是我想插入它..我想知道当用户完成此表格..我想知道注册日期..只是这一点。 – 2012-04-22 12:13:54

+0

为什么不直接设置表单结果的日期?像'Model.Date = DateTime.Now' – Manatherin 2012-04-22 12:19:05