2013-02-07 71 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); 
     } 
}