开发者

To Accept value from other control in same cshtml page in developing .Net MVC3 with Razor?

I think it must be answered alread开发者_StackOverflowy!

Something like :

  @Html.EditorFor(model => model.UserDept)

Just the UserDept has few options, so I want to be get choices by users in radio or checkbox and this textbox can get the value, :)


It's usually done with dropdown list, because there is a helper for it.

First, you have to populate data for that select list in your controller, like this:

public ActionResult Edit(int id)
{
    ViewBag.PossibleMembers = memberRepository.All;
    return View(projectRepository.Find(id));
}

Then in your view, you can use:

@Html.DropDownListFor(model => model.MemberId, ((IEnumerable<RunDog2.Models.Member>)ViewBag.PossibleMembers).Select(option => new SelectListItem {
    Text = (option == null ? "None" : option.Name), 
    Value = option.MemberId.ToString(),
    Selected = (Model != null) && (option.MemberId == Model.MemberId)
}), "Choose...")

You can get this code generated with MvcScaffolding for example.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜