私はMVC3とC#にスーパーです。私はこれとほぼ一日一緒に苦労しており、誰かが光を当てることを願っています(私は手がかり、ヒント、回答のためにこのサイトを洗ったことがあります)。LINQ/MVC3/C#の問題を使用してドロップダウンリストをバインドするには
私のデータベースには、すべてのデータを保持する単一のテーブルがあります。これは、ユーザーが選択時にタイムキーピング入力フォームに自動的に入力できる値を格納するプロファイルエディタ用です。最初のドロップダウンリストにプロファイル名を入力しようとしていますが、ステップ1に入っています。私はLINQ、MVC3/Razer、C#を使用しています。ここで
は私のDBMLです: カントポスト画像は、私がここで
http://imageshack.us/photo/my-images/560/timem.png/
新しいです原因私のモデルである:ここでは
namespace Timekeeping.Models
{
public class Profile
{
public int Profile_ID { get; set; }
public string profilename { get; set; }
public string networkuserid { get; set; }
public int projectnumber { get; set; }
public int costcode { get; set; }
public int paycode { get; set; }
public int jobtype { get; set; }
public int workorder { get; set; }
public int activity { get; set; }
public string taxarea { get; set; }
public IEnumerable<Profile> profiles { get; set; }
}
public class ProfileViewModel
{
public int Profile_ID { get; set; }
public IEnumerable<SelectListItem> profiles { get; set; }
}
}
は私のコントローラである:
namespace Timekeeping.Controllers
public class TimeProfileController : Controller
{
private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;
[HttpGet]
public ActionResult ProfileSelect()
{
profileConnectionDataContext dataContext = new profileConnectionDataContext(strConnString);
var model = new ProfileViewModel();
var rsProfile = from fbs in dataContext.TimeProfiles select fbs;
ViewData["ProfileList"] = new SelectList(rsProfile, "Profile_ID", "profilename");
return View(model);
}
}
そしてここに私が試したさまざまなhtmlヘルパーがあります私のビュー(なしの作業):
@Html.DropDownList("rsProfile", (SelectList)ViewData["ProfileList"]))
@Html.DropDownListFor(
x => x.Profile_ID,
new SelectList(Model.profiles, "Values", "Text"),
"-- Select--"
)
@Html.DropDownListFor(model => model.Profile_ID, Model.profilename)
私はこれを見て混乱ですけど、私は誰かが私は、ハードの部分に乗ることができます助けることができる期待しています。
public class TimeProfileController : Controller
{
private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;
[HttpGet]
public ActionResult ProfileSelect()
{
var dataContext = new profileConnectionDataContext(strConnString);
var profiles = dataContext.TimeProfiles.ToArray().Select(x => new SelectListItem
{
Value = x.Profile_ID,
Text = x.profilename
});
var model = new ProfileViewModel
{
profiles = profiles
};
return View(model);
}
}
とビューで::私はコミュニティ
値は= fbs.Profile_ID、ですText = fbs.profilename fbsは宣言されていないので、どのように使用できますか?あなたの助けの男のためにTY – Alex
@Alex、申し訳ありません、私はここで直接テストせずにコードを入力するとどうなりますか?それは 'fbs'ではなく' x'でなければなりません。 –
例外の詳細:System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。 51行:@ Html.DropDownListFor(ビューを返されたとき – Alex