だから私はasp.net mvc 4プロジェクトで働いていて、私のビューに問題があります.2つの異なるタイプのモデルを持つビューを作成したいのですが、最初のビュー(インデックス)はIEnumerable(Models.myModel)を2番目(サブスクライバーの詳細) Models.myModel を使用します。これは私のモデル コードです。
public class SubscribersModel
{
[Required]
[DataType(DataType.Text)]
public string name { get; set; }
[Required]
[DataType(DataType.Text)]
[StringLength(maximumLength: 10, MinimumLength = 7)]
public string cin { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime birthday { get; set; }
[DataType(DataType.Text)]
public string birthplace { get; set; }
}
私のコントローラーコード:
public class SubscribersController : Controller
{
private AgencyDbEntities dbcontext = new AgencyDbEntities();
private Subscribe sb = new Subscribe();
public ActionResult Index()
{
var subscribers = from sb in dbcontext.Subscribes select new SubscribersModel { cin = sb.cin, name = sb.name, birthday = (DateTime)sb.birthDay, birthplace = sb.birthPlace };
return View(subscribers);
}
public ActionResult Details(string id,string cin,string name)
{
var subscriber = new SubscribersModel();
IEnumerable<Subscribe> list = from s in dbcontext.Subscribes select s;
foreach (var sb in list)
{
if (sb.cin == id)
{
subscriber.cin = sb.cin;
subscriber.name = sb.name;
subscriber.birthday = (DateTime)sb.birthDay;
subscriber.birthplace = sb.birthPlace;
}
}
return View(subscriber);
}
}
私のインデックスビュー:
@model IEnumerable<_3SDWebProject.Models.SubscribersModel>
@{
ViewBag.Title = "Subscribers";
}
<div class="sidebar">
//here i want to show my details view
</div>
<div class="content" style="width: 700px; margin-left: 250px; height: 545px;margin- top: -30px;">
<h2>Subscribers</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@Styles.Render("~/Content/css")
<script src="@Url.Content("~/Scripts/Table.js")" type="text/javascript"></script>
<table class="altrowstable" id="alternatecolor">
<tr>
<th>
CIN
</th>
<th>
Name
</th>
<th>
birthday
</th>
<th>
birthplace
</th>
<th class="operations">
Operations
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.cin)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.birthday)
</td>
<td>
@Html.DisplayFor(modelItem => item.birthplace)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.cin }, new { @class = "details-logo"})
</td>
</tr>
}
</table>
</div>
私の詳細ビュー:
@model _3SDWebProject.Models.SubscribersModel
<fieldset>
<legend>SubscribersModel</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.name)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.name)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.birthday)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.birthday)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.birthplace)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.birthplace)
</div>
</fieldset>
誰かがアイデアや解決策を持っていれば、私は非常に感謝しています: 注: これは私が欲しいものを説明する写真です