2013-02-07 39 views
1

好吧,我使用的是MVC框架。我有添加模型的观点。目前我正在使用默认的“创建”控制器。添加用户输入和硬编码值的模型

我希望能够用我自己的变量预先创建一个模型。例如model.UserId我想设置为用户ID。我想要一些值由用户输入,我想要一些已经设置。有没有一种方法,我可以做这样的事情

(伪代码)

model.matchId = 123 
model.prediction type = "user input" 
add model 

这里如下

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>Predictions</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.MatchId, "Match") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("MatchId", String.Empty) 
     @Html.ValidationMessageFor(model => model.MatchId) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.UserId, "User") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("UserId", String.Empty) 
     @Html.ValidationMessageFor(model => model.UserId) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Type) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Type) 
     @Html.ValidationMessageFor(model => model.Type) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Prediction) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Prediction) 
     @Html.ValidationMessageFor(model => model.Prediction) 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

}

回答

0

我当前的代码在控制器中,您将设置将模型中的值返回给View之前的值。

public class HomeController : Controller 
    { 
     public ActionResult About() 
     { 
      var model = new MyModel(); 
      model.SomeId = 123; 
      model.SomeOtherProperty = "Hello World"; 
      return View(model); 
     } 
}