0

I've created a page that uses entity framework to retrieve/Display info in the database I've also got create functionality in a partial view on the same page but it doesn't save/insert data.

Here is my Home View:

@model IEnumerable<TerminalHost.Models.buildingInfo>

@{
ViewBag.Title = "Home Page";
}
<h3>We suggest the following:</h3>
<ol class="round">
<li class="one">
    <h5>Please begin creating your network structure:</h5> <button id="modal-opener">Add a new building</button>

</li>
<li class="two">

</li>
</ol>

<div class="Container">

<div class="Box1">
    @foreach (var item in Model)
        {<div>
            <h4 class="Heading">@item.buildingName</h4>
            <h4 class="Heading">@item.buildingNumber</h4>
            <p>@item.buildingDesc1</p>
            <p>@item.buildingDesc2</p>
            <p>@item.buildingDesc3</p>
        </div>
        }
</div>
</div>

<div id="Modal" title="Building Details">
@Html.Partial("buildForm", new TerminalHost.Models.buildingInfo())
</div>

Here is my partial View:

@model TerminalHost.Models.buildingInfo

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
<legend>buildingInfo</legend>

<div class="editor-label">
    @Html.LabelFor(model => model.buildingNumber)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.buildingNumber)
    @Html.ValidationMessageFor(model => model.buildingNumber)
</div>
....
</div>
<p>
    <input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index") 
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

And This is my controller:

public class HomeController : Controller
{
    thDB _db = new thDB();

    public ActionResult building()
    {
        var buildingModel = _db.buildings.ToList();//you have to remove the FirstOrDefault() extension method

        return View(buildingModel);
    }

    public ActionResult buildForm()
    {
        return PartialView();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult buildForm(buildingInfo buildinginfo)
    {
        if (ModelState.IsValid)
        {
            _db.buildings.Add(buildinginfo);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        return PartialView(buildinginfo);
    }


    protected override void Dispose(bool disposing)
    {
        if(_db != null)
        {
            _db.Dispose();
        }
        base.Dispose(disposing);
    }
}

I've attempted to replicate the same code as the scaffold template that is created to do the CRUD functions but they are separated out into different views whereas I want them all on one page.

1
  • Start by using the debugger. Does it enter the buildForm(buildingInfo buildinginfo) at all? Commented May 11, 2014 at 16:29

1 Answer 1

3

What you need it's to tell your partial view the Action to call. Could you change this line:

 @using (Html.BeginForm())

To

 @using (Html.BeginForm("builForm","Home"))

And in your Action:

HttpPost]
[ValidateAntiForgeryToken]
public ActionResult buildForm(buildingInfo buildinginfo)
{
    if (ModelState.IsValid)
    {
        _db.buildings.Add(buildinginfo);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

    return PartialView(buildinginfo);//instead of return this you can redirect to Index so you can see the update. Just a suggestion
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.