Last active
April 19, 2019 13:05
-
-
Save 16pxdesign/e2fd7f1710d56ae5a2d72c6e137fcd41 to your computer and use it in GitHub Desktop.
Nested #Form List passing #model in #ASP.NET #ASP.CORE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Application.Data.Models | |
@using Application.ViewModels | |
@model List<HealthIssueViewModel> | |
@(ViewData["PropertyName"]) | |
<div class="col-md mt-4"> | |
<div class="card bg-transparent"> | |
<div class="card-body"> | |
<div> | |
<input id="btnAddItem" type="button" | |
onclick="addRow@(ViewData["PropertyName"])();" value="Add Item"/> | |
</div> | |
<table> | |
<thead> | |
<tr> | |
<td> | |
Title | |
</td> | |
<td> | |
Published Date | |
</td> | |
</tr> | |
</thead> | |
<tbody id="table@(ViewData["PropertyName"])"> | |
@for (var i = 0; i < Model.Count; i++) | |
{ | |
<tr id="trItem@(ViewData["PropertyName"])@(i)"> | |
<td style='display:none'><input name='@(ViewData["PropertyName"]).Index' type='hidden' value='@(i)' /></td> | |
<td><input name='@(ViewData["PropertyName"])[@(i)].Flat' type='text' value='@Model[i].Date' /></td> | |
<td><input name='@(ViewData["PropertyName"])[@(i)].Street' type='text' value='@Model[i].Name' /></td> | |
<td><input type="hidden" name='@(ViewData["PropertyName"])[@(i)].Description' type='text' value='@Model[i].Description' /></td> | |
<td><a href="#" class="btn btn-link text-danger" id='btnAddItem' onclick='removeRow@(ViewData["PropertyName"])(@(i));' ><i class="fas fa-trash-alt"></i></a></td> | |
</tr> | |
} | |
</tbody> | |
</table> | |
<script language="javascript" type="text/javascript"> | |
function addRow@(ViewData["PropertyName"])() { | |
@{ | |
//TODO: | |
//For validation? Check it | |
Model.Add(new HealthIssueViewModel()); | |
} | |
var index = $("#table@(ViewData["PropertyName"])").children("tr").length; | |
var indexCell = "<td style='display:none'><input name='@(ViewData["PropertyName"]).Index' type='hidden' value='" + index + "' /></td>"; | |
var value1 = "<td><input name='@(ViewData["PropertyName"]).[" + index + "].Date' type='text' value='' /></td>"; | |
var value2 = "<td><input name='@(ViewData["PropertyName"]).[" + index + "].Name' type='text' value='' /></td>"; | |
var value3 = "<td><input type='hidden' name='@(ViewData["PropertyName"]).[" + index + "].Name' type='text' value='' /></td>"; | |
var removeCell = "<td><a href='#' class='btn btn-link text-danger' id='btnAddItem' onclick='removeRow@(ViewData["PropertyName"])(" + index + ");' ><i class='fas fa-trash-alt'></i></a></td>"; | |
var newRow = "<tr id='trItem@(ViewData["PropertyName"])" + index + "'>" + indexCell + value1 + value2 + value3 + removeCell + "</tr>"; | |
$("#table@(ViewData["PropertyName"])").append(newRow); | |
} | |
function removeRow@(ViewData["PropertyName"])(id) { | |
var controlToBeRemoved = "#trItem@(ViewData["PropertyName"])" + id; | |
$(controlToBeRemoved).remove(); | |
} | |
</script> | |
@{ | |
ViewData["PropertyName"] = "Kurwy"; | |
} | |
@ViewData["PropertyName"] | |
@{ | |
if (Model[0].Kurwy!=null) | |
{ | |
Html.RenderPartial("~/Views/Partial/AddOrEdit/HealthIssues.cshtml", Model[0].Kurwy, ViewData); | |
} | |
} | |
</div> | |
</div> | |
</div> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using Application.ViewModels; | |
using Microsoft.AspNetCore.Mvc; | |
namespace Application.Controllers | |
{ | |
public class TestController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
var test = new Test(); | |
test.Address = new List<AddressViewModel>(); | |
test.Address.Add(new AddressViewModel() | |
{ | |
City = "szczecin", | |
Flat = "100", | |
Postcode = "DD23AD", | |
Street = "High" | |
}); | |
test.Address.Add(new AddressViewModel() | |
{ | |
City = "szczecin", | |
Flat = "200", | |
Postcode = "DD23AD", | |
Street = "High" | |
}); | |
test.Address.Add(new AddressViewModel() | |
{ | |
City = "szczecin", | |
Flat = "300", | |
Postcode = "DD23AD", | |
Street = "High" | |
}); | |
return View(test); | |
} | |
[HttpPost] | |
public string Index(Test model) | |
{ | |
var sb = new StringBuilder(); | |
try | |
{ | |
sb.AppendFormat("Author : {0}", model.Name); | |
sb.AppendLine("<br />"); | |
sb.AppendLine("--------------------------------"); | |
sb.AppendLine("<br />"); | |
foreach (var item in model.Address) | |
{ | |
sb.AppendFormat("Title : {0} | Published Date : {1}", item.Flat, item.Street); | |
sb.AppendLine("<br />"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception(); | |
} | |
return sb.ToString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Application.Models; | |
using Microsoft.AspNetCore.Mvc; | |
using Newtonsoft.Json; | |
namespace Application.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
List<Contact> list = new List<Contact>(); | |
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") | |
{ | |
TempData.TryGetValue("data", out object value); | |
var data = value as string ?? ""; | |
list = JsonConvert.DeserializeObject<List<Contact>>(data) ?? new List<Contact>(); | |
TempData["data"] = JsonConvert.SerializeObject(list); | |
return PartialView("_Table", list); | |
} | |
TempData["data"] = JsonConvert.SerializeObject(list); | |
return View(list); | |
} | |
public IActionResult Contact() | |
{ | |
var model = new Contact { }; | |
return PartialView("_ContactModalPartial", model); | |
} | |
[HttpPost] | |
public IActionResult Contact(Contact model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
TempData.TryGetValue("data", out object value); | |
var data = value as string ?? ""; | |
List<Contact> list = JsonConvert.DeserializeObject<List<Contact>>(data) ?? new List<Contact>(); | |
list.Add(model); | |
string json = JsonConvert.SerializeObject(list); | |
TempData["data"] = json; | |
} | |
return PartialView("_ContactModalPartial", model); | |
} | |
[NonAction] | |
private void CreateNotification(string message) | |
{ | |
TempData.TryGetValue("Notifications", out object value); | |
var notifications = value as List<string> ?? new List<string>(); | |
notifications.Add(message); | |
TempData["Notifications"] = notifications; | |
} | |
public IActionResult Notifications() | |
{ | |
TempData.TryGetValue("Notifications", out object value); | |
var notifications = value as IEnumerable<string> ?? Enumerable.Empty<string>(); | |
return PartialView("_NotificationsPartial", notifications); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Application.Data.Models | |
@using Application.ViewModels | |
@model List<HealthIssueViewModel> | |
@(ViewData["PropertyName"]) | |
<div class="col-md mt-4"> | |
<div class="card bg-transparent"> | |
<div class="card-body"> | |
<div> | |
<input id="btnAddItem" type="button" | |
onclick="addRow@(ViewData["PropertyName"])();" value="Add Item"/> | |
</div> | |
<table> | |
<thead> | |
<tr> | |
<td> | |
Title | |
</td> | |
<td> | |
Published Date | |
</td> | |
</tr> | |
</thead> | |
<tbody id="table@(ViewData["PropertyName"])"> | |
@for (var i = 0; i < Model.Count; i++) | |
{ | |
<tr id="trItem@(ViewData["PropertyName"])@(i)"> | |
<td style='display:none'><input id='@(ViewData["PropertyName"]).Index' name='@(ViewData["PropertyName"]).Index' type='hidden' value='@(i)' /></td> | |
<td><input id='@(ViewData["PropertyName"])[@(i)].Flat' name='@(ViewData["PropertyName"])[@(i)].Flat' type='text' value='@Model[i].Date' /></td> | |
<td><input id='@(ViewData["PropertyName"])[@(i)].Street' name='@(ViewData["PropertyName"])[@(i)].Street' type='text' value='@Model[i].Name' /></td> | |
<td><input type="hidden" id='@(ViewData["PropertyName"])[@(i)].Description' name='@(ViewData["PropertyName"])[@(i)].Description' type='text' value='@Model[i].Description' /></td> | |
<td><a href="#" class="btn btn-link text-danger" id='btnAddItem' onclick='removeRow@(ViewData["PropertyName"])(@(i));' ><i class="fas fa-trash-alt"></i></a></td> | |
</tr> | |
} | |
</tbody> | |
</table> | |
<script language="javascript" type="text/javascript"> | |
function addRow@(ViewData["PropertyName"])() { | |
@{ | |
//TODO: | |
//For validation? Check it | |
Model.Add(new HealthIssueViewModel()); | |
} | |
var index = $("#table@(ViewData["PropertyName"])").children("tr").length; | |
var indexCell = "<td style='display:none'><input id='@(ViewData["PropertyName"]).Index' name='@(ViewData["PropertyName"]).Index' type='hidden' value='" + index + "' /></td>"; | |
var value1 = "<td><input id='@(ViewData["PropertyName"]).[" + index + "].Date' name='@(ViewData["PropertyName"]).[" + index + "].Date' type='text' value='' /></td>"; | |
var value2 = "<td><input id='@(ViewData["PropertyName"]).[" + index + "].Name' name='@(ViewData["PropertyName"]).[" + index + "].Name' type='text' value='' /></td>"; | |
var value3 = "<td><input type='hidden' id='@(ViewData["PropertyName"]).[" + index + "].Name' name='@(ViewData["PropertyName"]).[" + index + "].Name' type='text' value='' /></td>"; | |
var removeCell = "<td><a href='#' class='btn btn-link text-danger' id='btnAddItem' onclick='removeRow@(ViewData["PropertyName"])(" + index + ");' ><i class='fas fa-trash-alt'></i></a></td>"; | |
var newRow = "<tr id='trItem@(ViewData["PropertyName"])" + index + "'>" + indexCell + value1 + value2 + value3 + removeCell + "</tr>"; | |
$("#table@(ViewData["PropertyName"])").append(newRow); | |
} | |
function removeRow@(ViewData["PropertyName"])(id) { | |
var controlToBeRemoved = "#trItem@(ViewData["PropertyName"])" + id; | |
$(controlToBeRemoved).remove(); | |
} | |
</script> | |
@{ | |
ViewData["PropertyName"] = "Health[0].Kurwy"; | |
} | |
@ViewData["PropertyName"] | |
@{ | |
if (Model[0].Kurwy!=null) | |
{ | |
Html.RenderPartial("~/Views/Partial/AddOrEdit/HealthIssues.cshtml", Model[0].Kurwy, ViewData); | |
} | |
} | |
</div> | |
</div> | |
</div> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.codeproject.com/Tips/898017/Different-Ways-to-Pass-Data-to-Partial-View |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewData["PropertyName"] = "Health[0].Kurwy"; | |
} | |
@ViewData["PropertyName"] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Application.ViewModels | |
@model Application.ViewModels.Test | |
@{ | |
ViewBag.Title = "title"; | |
Layout = "_Layout"; | |
} | |
@using (Html.BeginForm()) | |
{ | |
@Html.ValidationSummary(true) | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Name) | |
</div> | |
<div class="editor-field"> | |
<input asp-for="Name" class="form-control"/> | |
@Html.ValidationMessageFor(model => model.Name) | |
</div> | |
<div> | |
<input id="btnAddItem" type="button" | |
onclick="addRow();" value="Add Item"/> | |
</div> | |
<table> | |
<thead> | |
<tr> | |
<td> | |
Title | |
</td> | |
<td> | |
Published Date | |
</td> | |
</tr> | |
</thead> | |
<tbody id="tbItems"> | |
@for (var i = 0; i < Model.Address.Count; i++) | |
{ | |
<tr id="trItem@(i)"> | |
<td style='display:none'><input name='Address.Index' type='hidden' value='@(i)' /></td> | |
<td><input name='Address[@(i)].Flat' type='text' value='@Model.Address[i].Flat' /></td> | |
<td><input name='Address[@(i)].Street' type='text' value='@Model.Address[i].Street' /></td> | |
<td><a href="#" class="btn btn-link text-danger" id='btnAddItem' onclick='removeRow(@(i));' ><i class="fas fa-trash-alt"></i></a></td> | |
</tr> | |
} | |
</tbody> | |
</table> | |
<p> | |
<input type="submit" value="Create" /> | |
</p> | |
} | |
<script language="javascript" type="text/javascript"> | |
function addRow() { | |
@{ | |
//TODO: | |
//For validation? Check it | |
Model.Address.Add(new AddressViewModel()); | |
} | |
var index = $("#tbItems").children("tr").length; | |
var indexCell = "<td style='display:none'><input name='Address.Index' type='hidden' value='" + index + "' /></td>"; | |
var titleCell = "<td><input name='Address[" + index + "].Flat' type='text' value='' /></td>"; | |
var publishedCell = "<td><input name='Address[" + index + "].Street' type='text' value='' /></td>"; | |
var removeCell = "<td><a href='#' class='btn btn-link text-danger' id='btnAddItem' onclick='removeRow(" + index + ");' ><i class='fas fa-trash-alt'></i></a></td>"; | |
var newRow = "<tr id='trItem" + index + "'>" + indexCell + titleCell + publishedCell + removeCell + "</tr>"; | |
$("#tbItems").append(newRow); | |
} | |
function removeRow(id) { | |
var controlToBeRemoved = "#trItem" + id; | |
$(controlToBeRemoved).remove(); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment