Person クラスと Contact クラスがあります。人は多くの連絡先を持つことができます。
public class Person
{
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public string Notes { get; set; }
[Required]
public List<Contact> Contacts { get; set; }
public Person()
{
Contacts = new List<Contact>();
}
}
public class Contact
{
[Required]
public string Title { get; set; }
[Required]
public string Value { get; set; }
public Contact() { Title = ""; Value = ""; }
public Contact(string title, string value)
{
Title = title;
Value = value;
}
}
コントローラー:
public class PersonController : Controller
{
public ActionResult Create()
{
return View(new PersonCreateModel());
}
// POST: /Person/Create
[HttpPost]
public ActionResult Create(string btnSubmit, PersonCreateModel personModel)
{
try
{
switch (btnSubmit)
{
case "AddContact":
personModel.Person.Contacts.Add(new Contact(personModel.NewContact_Title, personModel.NewContact_Value));
personModel.NewContact_Title = personModel.NewContact_Value = "";
return View(personModel);
case "CreatePerson"://Add To Database
//blabla
break;
}
return RedirectToAction("Index");
}
catch
{
return View(personModel);
}
}
}
PersonCreateModel :
public class PersonCreateModel
{
public Person Person { get; set; }
public string NewContact_Title { get; set; }
public string NewContact_Value { get; set; }
public PersonCreateModel()
{
Person = new Person();
}
}
ビュー:
@model MvcApplication1.Models.PersonCreateModel
@{
ViewBag.Title = "Create";
var contactsGrid = new WebGrid(Model.Person.Contacts);
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Person.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Person.FirstName)
@Html.ValidationMessageFor(model => model.Person.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Person.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Person.LastName)
@Html.ValidationMessageFor(model => model.Person.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Person.Notes)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Person.Notes)
@Html.ValidationMessageFor(model => model.Person.Notes)
</div>
<br />
<div>
<h4>Contacts:</h4>
<table>
<tr>
<td>Title: @Html.EditorFor(model => model.NewContact_Title)</td>
<td>Value: @Html.EditorFor(model => model.NewContact_Value)</td>
<td>
<input type="submit" name="btnSubmit" value="AddContact" /></td>
</tr>
</table>
<div>
@contactsGrid.GetHtml()
</div>
</div>
<p>
<input type="submit" name="btnSubmit" value="CreatePerson" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
私が達成しようとしているのは、ユーザーが最終的に [作成] をクリックする前に、複数の連絡先を Person に追加できることです。
2 つの質問があります。
1- これは最善の方法ですか?、javascript や jquery を使用するなど、これを行うためのより簡単で短い方法はありませんか?
2- AddContact をクリックすると、最初はうまく機能し、2 回目は Person.Contacts が空になり、AddContact テキストボックスをクリアできません。
ウェブ全体とここで Stackoverflow を検索しましたが、答えが見つかりませんでした。ここに未回答の質問があります
PS: ASP.NET Webforms から来て、MVC は初めてです。