Reputation: 59
I want to add a create function to my page, it has a buildings page and a rooms page however depending on which building is chosen rooms corresponding to its ID are shown. I have that completed but I wanted to add a create function to add more rooms when in a specific building. I have all the required code for creating/inserting in a partial view but I need to pass in relational value without having to put it in myself. Similar to this action link:
@Html.ActionLink("Edit", "Edit", **new { id=item.Id }**)
Here is my room page:
@model TerminalHost.Models.buildingInfo
@{
ViewBag.Title = "Home Page";
}
<h3>Rooms in: @Model.buildingName</h3>
<ol class="round">
<li class="one">
<h5>Please begin creating your network structure:</h5> <button id="modal-opener">Add a new room</button>
</li>
<li class="two">
</li>
</ol>
@*rooms that are specific to a building are shown here*@
@Html.Partial("rooms", Model.roomId)
<div id="Modal" title="room Details">
@Html.Partial("roomForm", new TerminalHost.Models.roomInfo())
</div>
my displayed rooms partial view:
@model IEnumerable<TerminalHost.Models.roomInfo>
<div class="Container">
@foreach (var item in Model)
{<div class="Box">
<div>
<h4 class="Heading">@item.roomName</h4>
<h4 class="Heading">@item.roomNumber</h4>
<p>@item.roomDesc1</p>
<p>@item.roomDesc2</p>
<p>@item.roomDesc3</p>
</div>
</div>
}
</div>
my roomForm partial view:
@model TerminalHost.Models.roomInfo
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("roomForm", "Home"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>roomInfo</legend>
<div class="editor-label">
@Html.LabelFor(model => model.roomNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.roomNumber)
@Html.ValidationMessageFor(model => model.roomNumber)
</div>
....
//Here I would like the building Id to be passed automatically from the buildingInfo model which is being called in the rooms view
<div class="editor-label">
@Html.LabelFor(model => model.buildingId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.buildingId)
@Html.ValidationMessageFor(model => model.buildingId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
This is the model that I'm using to store data with:
public class buildingInfo
{
public int Id { get; set; }
public int buildingNumber { get; set; }
public String buildingName { get; set; }
public String buildingDesc1 { get; set; }
public String buildingDesc2 { get; set; }
public String buildingDesc3 { get; set; }
public virtual ICollection<roomInfo> roomId { get; set; }
}
Here is the roomInfo model:
public class roomInfo
{
public int Id { get; set; }
public int roomNumber { get; set; }
public String roomName { get; set; }
public String roomDesc1 { get; set; }
public String roomDesc2 { get; set; }
public String roomDesc3 { get; set; }
public int buildingId { get; set; }
//public virtual ICollection<rackInfo> rackId { get; set; }
}
If anymore information is needed please ask and I will provide it.
Upvotes: 2
Views: 513
Reputation:
I have seen the other Question. But I prefer to answer here maybe it will solve the second problem. Because there are many solutions. But I think maybe the first I gave doesn't allow you to populate this value for some reason.
Could you try this:using a overload of Html.Partial()
method and passing a ViewDataDictionary
with the value you want.
You need to change this line:
@Html.Partial("roomForm", new TerminalHost.Models.roomInfo())
To:
@Html.Partial("roomForm", new TerminalHost.Models.roomInfo(), new ViewDataDictionary { {"buildingId", Model.buildingId} } )
And in your partial view, you can retrieve it like this:
@model TerminalHost.Models.roomInfo
@{
ViewBag.Title = "Create";
}
@{
int buildingId = Convert.ToInt32(ViewData["buildingId"]);
}
........
<div class="editor-field">
@Html.TextBoxFor(model => model.buildingId,new {@Value=buildingId })
@Html.ValidationMessageFor(model => model.buildingId)
</div>
I hope by doing in this way it will solve the second problem.
Upvotes: 0
Reputation:
I was thinking to a solution using ViewDataDictionary
but I just realised you can pass the value through the constructor of your object.
So you can try this:
@Html.Partial("roomForm",new TerminalHost.Models.roomInfo { buildingId=Model.Id })
I hope it will help you.
Upvotes: 1