Tuesday, March 31, 2015

Sample partial view

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace partialView.Models
{
    public class Person
    {
        public int id { get; set; }
        public string nameLast { get; set; }
        public Address address { get; set; }
       
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace partialView.Models
{
    public class Address
    {
        public string streeNo { get; set; }
        public string city { get; set; }
        public string state { get; set; }
    }


}

Home/Index.cshtml

@model partialView.Models.Person
<h2>Index</h2>
@{
    ViewBag.Title = "Index";
}
<div>@Html.DisplayFor(model => model.nameLast)</div>
<div>@Html.DisplayFor(model => model.address.streeNo)</div>
<div>@Html.DisplayFor(model => model.address.city)</div>
<div>@Html.DisplayFor(model => model.address.state)</div>

@Html.Partial("_AddressEdit",Model.address)

@Html.ActionLink("edit address","edit","Person",new { id=Model.id }, null);

@Html.ActionLink("edit address", "edit", "Person", new { id = Model.id }, null);

PersonController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using partialView.Models;

namespace partialView.Controllers
{
    public class PersonController : Controller
    {
        // GET: Person
     
        public ActionResult AddressEdit(int id)
        {
            return View("_Address");
        }


        public ActionResult Index()
        {
            Person person = new Person();
            person.nameLast = "chu";
            Address addr = new Address();
            addr.city = "columbus";
            addr.state = "oh";
            addr.streeNo = "101";
            person.address = addr;

            return View();
        }

        // GET: Person/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Person/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Person/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Person/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Person/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Person/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Person/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

Partialview
_AddressEdit.chtml

@model partialView.Models.Address


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Address</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.streeNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.streeNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.streeNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.city, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.city, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.city, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.state, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.state, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.state, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

No comments:

Post a Comment