好的,所以我使用的是MVC框架。我有一个添加模型的视图。目前我使用的是默认的"创建"控制器。
我希望能够创建一个预先设置了自己变量的模型。例如模型。UserId我想设置为usersId。我希望一些值由用户输入,并且我希望一些已经设置。有没有办法我可以做这样的
(伪代码)
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>
}
在控制器中,您可以在将模型返回到视图之前设置模型上的值。
public class HomeController : Controller
{
public ActionResult About()
{
var model = new MyModel();
model.SomeId = 123;
model.SomeOtherProperty = "Hello World";
return View(model);
}
}