問題の少し後ろに、そして私がひどくやろうとしていること。基本的に、viewModelにデータを配置するフォームがあります。問題は、顧客の会社が1つの住所にあるが、連絡先が別の住所にあると言うことです。ユーザーが入力しているメインフォームにあるラジオボタンをクリックして、連絡先の別の住所を入力できるようにします。これをメインフォームにあるモデルに保存します。
私は次のことを行うコントローラーを持っています:
[HttpPost]
public ActionResult Edit(ClientModel client)
{
Domain.Data.EFDbContext context = new Domain.Data.EFDbContext();
if (ModelState.IsValid)
{
client.ClientAddress.AddressTypeId = client.AddressType.AddressTypeId;
client.Contact.ContactTypeId = client.ContactType.ContactTypeID;
//Add the address to the ContactAddress table and Contact ID
if (client.ContactAddress.AddressLine1 != null)
{
client.Contact.ContactAddress.Add(client.ContactAddress);
context.Contacts.Add(client.Contact);
}
else
{
client.ContactAddress = client.ClientAddress;
}
client.Company.Address.Add(client.ContactAddress);
client.Company.CompanyContact.Add(client.Contact);
//The attachment to Client is created on a Trigger in the database when a new Company is added
context.Companies.Add(client.Company);
context.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(client);
}
}
これは正常に機能し、データベーステーブルを更新して結果を保存しますが、client.ContactAddress部分に追加したので、フォームのフィールドが空であることがわかっている場合は、クライアントのある場所に会社のアドレスを割り当てて保存します。関連分野で。(ただし、これは問題ではありませんが、背景にあります)
私はpartialViewとコントローラーを作成しようとしました
[HttpGet]
public PartialViewResult AddressPartial()
{
return PartialView("_AddressPartial");
}
そして私のedit.aspxで私は持っています
$(document).ready(function () {
$('#Not_Contact_Address').click(function (e) {
if ($('#Not_Contact_Address').is(':checked')) {
$('#dialog').dialog({
autoOpen: false,
width: 400,
resizable: false,
title: 'Add Address',
modal: true,
open: function(event, ui) {
//Load the AddressPartial action which will return
// the partial view _AddressPartial
$(this).load("@Url.Action("AddressPartial")");
},
buttons: {
"Save": function() {
$('#dialog').dialog('close');
},
"Cancel": function () {
$('#dialog').dialog('close');
}
}
});
$('#dialog').dialog('open');
}
});
});
基本的に、ユーザーがラジオボタンを選択すると、[アドレスの追加]ダイアログがポップアップします。ユーザーにその情報を追加して、選択したラジオボタンの下のフォームにこの情報を表示し直してもらいます。その後、ユーザーは続行してヒットを完了することができます。 [送信]ボタンをクリックして、すべての情報をすぐにデータベースに保存します。(上のコントローラーに表示されています)。
<fieldset>
<legend>Main Contact Details</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.Phone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.Phone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Contact.Mobile)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Contact.Mobile)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ContactType.Name)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ContactType.ContactTypeID, Model.ContactSelectList, " --- Select One --- ", null)
</div>
<b>Contact Address</b> same as <b>Company Address</b> @Html.RadioButton("Not_Contact_Address", "Yes") Yes @Html.RadioButton("Not_Contact_Address", "No") No
</fieldset>
問題
正常なダイアログを開くことはできますが、ダイアログボックスをキャンセルすることはできません。右上隅のxをクリックすると、フォーム全体が空白になり、必要なページではなく空白のページが残ります。
ユーザーがフォームに情報を入力し、ページ内に別のフォームを表示して住所を追加し、それを私が持っているviewModelに保存して、ユーザーが続行できるようにするための最良の方法は、フォームを用意することです。
更新しました
function openProductEditDialog() {
$("#ProductDetailsDialogDiv").html("");
$.ajax({
type: "GET",
url: encodeURI('@Url.Action("AddressPartial", "Home")'),
cache: false,
dataType: 'html',
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#ProductEditDialogDiv").html(XMLHttpRequest.responseText);
},
success: function (data, textStatus, XMLHttpRequest) {
$("#ProductEditDialogDiv").html(data);
},
complete: function (XMLHttpRequest, textStatus) {
$('#ProductEditDialogDiv').dialog({
width: '500px',
modal: true
});
}
});
}
これにより、情報を新しいウィンドウに保存して画面に戻ることができます。これは、メインページにviewModelを保存する方法です。AddressPartialを、それが呼び出されたCreateClientページのviewModelに配置して、フォームを続行し、情報を失わないようにします。
これをコントローラーに向けて、ContactAddressを含むviewModelをCreateClientページに再送信するように指示する必要があると思います。現在、これに取り組んでいます。