私は剣道でMVCを使用しています。剣道グリッドの追加ボタンをクリックすると、ポップアップダイアログが開きます。モーダルダイアログには、2つのコンボボックスを含むフィールドがあります。ユーザーがこれらのフィールドに値を入力して[追加]ボタンをクリックすると、ViewModelが読み込まれます。 GetLanguages()およびGetCountries()アクションメソッドを使用してコンボボックスにデータを入力しています。したがって、これらの2つのcomboxはviewmodelを使用して生成されません。ビュー内に私のビューモデルのモデルフィールドを設定しました。たとえば、コンボボックスの値がビューモデルに追加されないように選択されました
@using System.Collections
@model CC.GRP.MCRequest.ViewModels.UserProfileViewModel
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
コンボボックスを選択すると、値がビューモデルに追加されません。私が何か間違っていると誰かが私に知らせることができますか?ビューモデルに国のリストコレクションを追加し、ビューに送信する前にコントローラ内のビューモデルにコレクションを移入する必要がありますか。それが事実なら、どうすればいいのですか?以下はUserProfile_Readが
コントローラ
public ActionResult GetLanguages()
{
return Json(MCRHelper.GetLanguages(), JsonRequestBehavior.AllowGet);
}
public ActionResult GetCountries()
{
return Json(MCRHelper.GetCountries(), JsonRequestBehavior.AllowGet);
}
public ActionResult UserProfile_Read([DataSourceRequest]DataSourceRequest request)
{
var users = mcrRepository.GetUserProfileById(0).AsQueryable().ProjectTo<UserProfileViewModel>();
var response = users.ToDataSourceResult(request);
return Json(response, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult UserProfile_Create([DataSourceRequest] DataSourceRequest request, UserProfileViewModel userVM)
{
if (!ModelState.IsValid)
{
return null;
}
if (userVM.UserProfileID == 0)
{
mcrRepository.CreateUserProfile(Mapper.Map<UserProfile>(userVM));
return Json(mcrRepository.GetTeams().ToDataSourceResult(request));
}
else
{
mcrRepository.UpdateUserProfile(Mapper.Map<UserProfile>(userVM));
}
return null;
}
リポジトリ
public void CreateUserProfile(UserProfile userProfile)
{
if (MCRHelper.UserValidate() == 1)
{
var userProfiles = db.spInsertUserProfile(userProfile.EmployeeID,
userProfile.Forename,
userProfile.Surname,
userProfile.PreferredName,
userProfile.DefaultLanguageCode,
userProfile.DefaultCountryCode,
userProfile.TimeZoneID,
userProfile.Domain,
userProfile.NetworkID,
userProfile.EmailAddress,
true,
MCRHelper.GetShortname());
}
}
画面のviewmodelを移入
<div class="editor-label">
@Html.LabelFor(model => model.Forename)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Forename)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Surname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Surname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PreferredName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PreferredName)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.DefaultLanguageCode)
// .Name("LanguageCode")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("LanguageName")
.DataValueField("DefaultLanguageCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetLanguages", "Admin").Type(HttpVerbs.Post))
)
)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DefaultCountryCode)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.DefaultCountryCode)
// .Name("CountryCode")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("Country")
.DataValueField("DefaultCountryCode")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetCountries", "Admin").Type(HttpVerbs.Post))
)
)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TimeZoneName)
</div>
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.TimeZoneName)
// .Name("TimeZoneID")
.HtmlAttributes(new { style = "width:300px" })
.DataTextField("TimeZoneName")
.DataValueField("TimeZoneID")
.DataSource(dataSource => dataSource
.Read(read => read.Action("GetTimeZones", "Admin").Type(HttpVerbs.Post))
)
)
</div>
私のコードです
ブレークポイントを追加し、コードをステップ実行してどれだけ遠くにいるのか確認しましたか?あなたのコントローラのメソッドは動作していますか? – DaniDev
getアクションメソッドはすべて正常に動作します。追加ダイアログボックスが開くと、コンボボックスにデータが入力されます。唯一の例外を保存している間に、言語と国のフィールドがnullであると言うnull例外エラーが発生します – Tom
A-Ha!保存アクションと例外メッセージを投稿してください。 – DaniDev