Post values by using a table on a view - c#

I have a table on my view and I pass a model to this view on tbody I get values from the model and a select where I get values from a viewbag. I want to relate the values that are on each row something like Metadisciplina.Nome with the selected value on ViewBag.Docente.... Per each row I will get something like a list I don't know. Right now I have a method where I get two different types of lists where in one I get the model and in the other the values from the select option... But using this two cycles and a lot of if's I just can have one of my ideas... Or I can get for the same Docente value each Metadisciplina.Nome or for each Metadisciplina.Nome get different Docente values... I want to have the possibilty to have both Metadisciplina.Nome with different Docente values and Metadisciplina.Nome with the same Docente values.. The user will decide that...
View
#model IList<ModelsLibrary.Turno>
#{
ViewData["Title"] = "Index";
}
<div class="row">
<div class="col-md-4">
<form asp-action="Index" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label>Ano Letivo</label>
<select name="anoLetivo" class="form-control" asp-items="ViewBag.Ano"></select>
</div>
<div class="form-group">
<label>Ano Curricular</label>
<select name="ano" class="form-control">
<option>-Select-</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<div class="form-group">
<label>Semestre</label>
<select name="semestre" class="form-control">
<option>-Select-</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="form-group">
<input type="submit" value="Escolhido" class="btn btn-default" />
</div>
</form>
</div>
</div>
<form asp-action="Create" method="post">
<table class="table">
<thead>
<tr>
<th>
<label>Disciplina</label>
</th>
<th>
<label>Turno</label>
</th>
<th>
<label>Tipologia</label>
</th>
<th>
<label>Docente</label>
</th>
</tr>
</thead>
<tbody>
#for (var i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(x => x[i].TurnoId)
#Html.HiddenFor(x => x[i].MetaDisciplina.Nome)
<tr>
<td>
#Html.DisplayFor(modelItem => modelItem[i].MetaDisciplina.Nome)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].LetraTurno)
</td>
<td>
#Html.DisplayFor(modelItem => modelItem[i].Tipologia.Tipo)
</td>
<td>
<select name="DocenteId" asp-items="ViewBag.Docente"></select>
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller Method
[HttpPost]
public async Task<IActionResult> Create(IList<Turno> Turno, IList<int> DocenteId)
{
var turnodocente = new List<TurnoDocente>();
foreach (var item in Turno)
{
foreach (var ite in DocenteId)
{
if(!turnodocente.Any(x=>x.DocenteId == ite && x.TurnoId == item.TurnoId))
{
turnodocente.Add(new TurnoDocente
{
TurnoId = item.TurnoId,
DocenteId = ite
});
}
}
}
HttpResponseMessage responsemeta = await HttpRequestBuilder.WebApiClient.PostAsJsonAsync("api/TurnoDocente", turnodocente);
responsemeta.EnsureSuccessStatusCode();
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Expected Result
I expect to get the values without the red cross not all.... This is one of the options.. In here I just have an example but I try another way to do the if clause but the result is not what I want... And and didn't really know how I can get the values that are related per each table row...

Related

The model directive may only occur once per document (adding buttons to lists problem) ASP.NET MVC Core C#

I'm trying to display a button because I need to add new information but there is a list within the file and it is not working
I am using ASP.NET Core MVC and C#.
It is something like this:
HTML File:
#model IEnumerable<MyApplication.Models.Clients>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.CODE)
</th>
<th>
#Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
My controller method:
public ActionResult Create()
{
List<Clients> clients = dbContext.GetClients().ToList();
return View(clients);
}
I want to add this in my HTML file:
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CODE" class="control-label"></label>
<input asp-for="CODE" class="form-control" />
<span asp-validation-for="CODE" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NAME" class="control-label"></label>
<input asp-for="NAME" class="form-control" />
<span asp-validation-for="NAME" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
I tried to add this:
#Model MyApplication.Models.Clients
I get this error:
The model directive may only occur once per document
How can I solve it?
EDIT:
My Client Model is this:
public class Clients{
public int CODE {get; set;}
public string NAME {get; set;}
}
If i put the button inside the HTML File it throws and error:
#model IEnumerable<MyApplication.Models.Clients>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.CODE)
</th>
<th>
#Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CODE" class="control-label"></label>
<input asp-for="CODE" class="form-control" />
<span asp-validation-for="CODE" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NAME" class="control-label"></label>
<input asp-for="NAME" class="form-control" />
<span asp-validation-for="NAME" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
It says:
IEnumerable<MyApplication.Models.Clients> does not contain a
definition CODE and no accesible extension method CODE
IEnumerable<MyApplication.Models.Clients> does not contain a
definition NAME and no accesible extension method NAME
EDIT #2
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label for="CODE" class="control-label"></label>
<input name="CODE" class="form-control" />
</div>
<div class="form-group">
<label for="NAME" class="control-label"></label>
<input name="NAME" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
EDIT #3:
Now it is throwing this error: Object reference not set to an instance of an object, in the foreach part:
#model IEnumerable<MyApplication.Models.Clients>
<div class="row">
<div class="col-md-4">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label for="CODE" class="control-label">Code:</label>
<input name="CODE" class="form-control" />
</div>
<div class="form-group">
<label for="NAME" class="control-label">Name:</label>
<input name="NAME" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Insert" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.CODE)
</th>
<th>
#Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
Image:
[![image][1]][1]
This is my Controller Create HTTP METHOD:
public ActionResult Create([Bind] Clients clients)
{
try
{
if (ModelState.IsValid)
{
dbContext.InsertData(clients);
return RedirectToAction("Index");
}
return View(clients);
}
catch
{
return View();
}
}
You cannot use asp-for="..." when the model/value you are using does not have these properties. Your model is defined as IEnumerable<MyApplication.Models.Clients> as the value for the view is a List<MyApplication.Models.Clients> object. When you use <input asp-for="CODE"> it looks for a property CODE inside the IEnumerable<...> type. However, this interface does not have such a property.
In your case you don't need asp-for="". Instead you can use a "normal" <input> tag with the name="..." attribute you want, depending on the Create() action you have defined. The code can look like this:
<div class="form-group">
<label for="CODE" class="control-label">Code:</label>
<input name="CODE" class="form-control" />
</div>
That way the HTTP Request will have the field name CODE in the POST body or GET URL parameter list, depending on how you send the form. ASP.NET will automatically fill the values into the parameters of your Create() action for you.

Problem with pass data to the controller with view model ( two list )

I create a view and create two cards to show new skill and current skill.
The problem is TittleNewSkillEHS back to the controller cero data after I select a new skill.
Short description of my request
I will need to display two lists with information about New Skill (required checkbox to select and add current skill), Current skill (required update target).
One approached to solution the request I create a view model take EmployeeTitle id, Title name, and two list
• List Current skills
• List New skills
.
.
.
TitleSkillVM TitleSkillVMNew = new TitleSkillVM();
TitleSkillVMNew.TitleId = SelectTitle.TitleId;
TitleSkillVMNew.TitleDescription = SelectTitle.TitleName;
TitleSkillVMNew.TittleNewSkillEHS = _tempNewEHS; “New skill”
TitleSkillVMNew.TittleSkillEHS = _tempEHS; “List Current skill”
return View(TitleSkillVMNew);
}
#model WCMApps.Models.ViewModel.TitleSkillVM
<div class="row">
<div class="col-sm-6">
<form asp-controller="AdmTitles" asp-action="InsertNewSkillTarget" method="post">
<input type="hidden" asp-for="TitleId" />
<%: Html.HiddenIndexerInputForModel() %>
<input type="hidden" asp-for="TittleNewSkillEHS" />
<div class=" card">
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>New Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th></th>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TittleNewSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-info">
<td>
#Html.CheckBoxFor(modelItem => item.IsSelectedNewSkillTitle, item.TitleSkillTargetId)
#Html.HiddenFor(modelItem => item.TitleSkillTargetId)
</td>
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
#Html.HiddenFor(modelItem => item.SkillLevelDDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
#Html.HiddenFor(modelItem => item.Target)
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="submit" class="btn btn-outline-info" onclick="location.href='#Url.Action("InsertNewSkillTarget", "AdmTitles", Model.TittleNewSkillEHS)'">Save</button>
</small>
</div>
</div>
</form>
</div>
<div class="col-sm-6">
<form asp-controller="AdmTitles" asp-action="UpdateCurrentSkillTarget" method="post">
<div class="card">
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>Current Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
<th scope="col">
Target
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TittleSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-success">
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
</td>
<td>
<div class="form-group">
<input asp-for="Target" class="form-control" />
<span asp-validation-for="Target" class="text-danger"></span>
</div>
</td>
</tr>
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="button" class="btn btn-outline-info" onclick="location.href='#Url.Action("UpdateCurrentSkillTarget", "AdmTitles", Model.TittleSkillEHS)'">Update</button>
</small>
</div>
</div>
</form>
</div>
</div>'
Here is a demo to pass TittleNewSkillEHS to Controller:
View:
<div class="card-header">
<div class="container">
<div class="row">
<div class="col-9">
<h4>New Skill Index</h4>
</div>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th></th>
<th scope="col">
Skill Name
</th>
<th scope="col">
Current Target
</th>
</tr>
</thead>
<tbody>
#{ var count = 0;}
#foreach (var item in Model.TittleNewSkillEHS)
{
if (item.TitleSkillTargetStatus == true)
{
<tr class="table-info">
<td>
#*#Html.CheckBoxFor(modelItem => item.IsSelectedNewSkillTitle, new { #name = "TittleNewSkillEHS[#count].IsSelectedNewSkillTitle" })
#Html.CheckBox("IsChecked", new { #name = "TittleNewSkillEHS[#count].IsSelectedNewSkillTitle" })*#
<input asp-for=#item.IsSelectedNewSkillTitle name="TittleNewSkillEHS[#count].IsSelectedNewSkillTitle"/>
<input name="TittleNewSkillEHS[#count].TitleSkillTargetId" value=#item.TitleSkillTargetId hidden />
</td>
<td>
#Html.DisplayFor(modelItem => item.SkillLevelDDescription)
<input name="TittleNewSkillEHS[#count].SkillLevelDDescription" value=#item.SkillLevelDDescription hidden />
#*#Html.HiddenFor(modelItem => item.SkillLevelDDescription)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.Target)
<input name="TittleNewSkillEHS[#count].Target" value=#item.Target hidden />
#*#Html.HiddenFor(modelItem => item.Target)*#
</td>
</tr>
count++;
}
}
</tbody>
</table>
</div>
<div class="card-footer">
<small class="text-muted">
<button type="submit" class="btn btn-outline-info">Save</button>
</small>
</div>
</div>
Controller:
public IActionResult InsertNewSkillTarget(TitleSkillVM t) {
return Ok();
}
result:

How do I pass a selected ID to my SQL Database?

I am having trouble with this last part of my project. I have all of my SQL Table columns filled up except for this last part. I have two tables WorkSchedule and WorkShiftBid.
In WorkShiftBid, I have WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID
I will be using Guid.NewGuid() for WorkShiftBidID, Datetime.now for WSBidDatetime, A hardcoded "pending" value for WSBidStatus, A viewbag for StaffID and I am trying to get the WorkScheduleID to work but to no avail.
For WorkSchedule, I am trying to get just the "WorkScheduleID" attribute just by using a button method asp-route-id="#item.WorkScheduleID" and it will redirect me to the Create page whereby I will select a Staff from the drop-down list and then save it to my WorkShiftBid table as it has a foreign key for "WorkScheduleID".
It would be helpful if someone could help me. Thank you.
Controller Code:
public IActionResult CreateWorkShift()
{
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel)
{
if (id != workShiftBidModel.WorkScheduleID)
{
return NotFound();
}
if (ModelState.IsValid)
{
workShiftBidModel.WSBidDateTime = DateTime.Now;
workShiftBidModel.WorkShiftBidID = Guid.NewGuid();
_context.Add(workShiftBidModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(DisplaySchedule));
}
ViewData["StaffID"] = new SelectList(_context.Staff, "StaffID", "StaffName", workShiftBidModel.StaffID);
return View(workShiftBidModel);
}
private bool WorkShiftBidModelExist(Guid id)
{
return _context.WorkSchedule.Any(e => e.WorkScheduleID == id);
}
Display WorkShift Page:
<h1 class="text-center"> Apply Workshifts</h1>
<hr />
<table class="table">
<thead>
<tr>
<th>
From (DateTime)
</th>
<th>
To (DateTime)
</th>
<th>
Day
</th>
<th>
Status
</th>
<th>
Work Descriptions
</th>
<th>
Branch
</th>
<th>
Manager
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleToDateTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleFromDateTime.DayOfWeek)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkScheduleStatus)
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkDescriptions.WorkDescriptionName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Branches.BranchName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Staff.StaffName)
</td>
<td>
<form method="post">
<input type="submit" class="btn btn-primary btn-block" value="Bid" asp-route-id="#item.WorkScheduleID" asp-action="DisplaySchedule" asp-controller="PartTimer" />
</form>
<div class="form-group col-md-6">
<a asp-action="CreateWorkShift" asp-route-id="#item.WorkScheduleID" class="btn btn-success">Create</a>
</div>
</td>
</tr>
}
</tbody>
</table>
Create view page code:
<h1>Create</h1>
<h4>Work Shifts</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreateWorkShift">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="WorkScheduleID"/>
<div class="form-group">
<label asp-for="WSBidStatus" class="control-label"></label>
<input readonly asp-for="WSBidStatus" value="Pending" class="form-control"/>
<span asp-validation-for="WSBidStatus" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="StaffID" class="control-label"></label>
<select asp-for="StaffID" class="form-control" asp-items="ViewBag.StaffID"></select>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="submit" value="Save" class="btn btn-primary btn-block" />
</div>
<div class="form-group col-md-6">
<a asp-action="ProfilePage" class="btn btn-secondary btn-block"><i class=" fa fa-table"></i>Back to List</a>
</div>
</div>
</form>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Do you mean you cannot get the WorkScheduleID in public async Task<IActionResult> CreateWorkShift(Guid? id, [Bind("WorkShiftBidID,WSBidDateTime,WSBidStatus,WorkScheduleID,StaffID")] WorkShiftBidModel workShiftBidModel).If so,you need to get and pass Id in public IActionResult CreateWorkShift.Here is a demo worked:
public IActionResult CreateWorkShift(int id)
{
List<Staff> list = new List<Staff> { new Staff { StaffID = 1, StaffName = "staff1" }, new Staff { StaffID = 2, StaffName = "staff2" } };
ViewData["StaffID"] = new SelectList(list, "StaffID", "StaffName");
WorkShiftBidModel w = new WorkShiftBidModel { WorkScheduleID = id };
return View(w);
}
result:

How to post data to razor page code behind asp.net core

I want to implement a bulk operation function, but I cannot post my data to the razor page code behind, anyone can help? Please check my code below
<form method="post" asp-page="./Index">
<div class="row">
<div class="col-md-12">
<table class="table table-hover table-bordered" style="margin-top:15px;">
<thead>
<tr class="info">
<th></th>
<th>
<a asp-page="./Index" asp-route-sortOrder="#Model.NumberSort">
#Html.DisplayNameFor(model => model.Products[0].Number)
</a>
</th>
<th>
#Html.DisplayNameFor(model => model.Products[0].Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Products)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.Checked)
</td>
<td class="success">
#Html.DisplayFor(modelItem => item.Number)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
<a asp-page="./Details" asp-route-id="#item.ID" class="btn btn-info">details</a>
<a asp-page="./Edit" asp-route-id="#item.ID" class="btn btn-warning">edit</a>
<a asp-page="./Delete" asp-route-id="#item.ID" class="btn btn-danger">delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-10">
#{
var items = Model.Products.Where(x => x.Checked);
}
<input type="submit" name="bulkUpload" value="bulk upload" asp-route-data="#items"/>
</div>
</div>
</form>
public async Task OnPostAsync(List<Product> upload)
{
}
You need to render your products in such way that the Modelbinder will be able to pick them up on form submit and pass them into the codebehind.
Something like this (done by hand so it might not compile at first):
#{
for (int i = 0; i < Model.Products.Count; i++)
{
<input type="checkbox" asp-for="Model.Produtcs[i].Checked" />
#Model.Products[i].Number
#Model.Products[i].Name
}
}
This will render HTML like
<input type="checkbox" name="Products[0].Checked" />
<input type="checkbox" name="Products[1].Checked" />
<input type="checkbox" name="Products[2].Checked" />
And it should get back to your OnPostAsync. If not then try changing
public async Task OnPostAsync(List<Product> upload)
to
public async Task OnPostAsync(YourModel model)
What ever your full model is named.
This is then redundant:
#{
var items = Model.Products.Where(x => x.Checked);
}

Child Actions are not allowed to perform redirect actions Partial Views

On my Main View I have 4 partial views.. two are tables.. the others are create forms.
Partial View Table 1
#model IEnumerable<ProjectName.Models.code_AutoMake>
<h3>Auto Make List</h3>
<table id="Auto-Make-Table" class="table table-bordered table-striped">
<thead>
<tr>
<th class="col-md-5">
#Html.DisplayNameFor(model => model.AutoMake)
</th>
<th class="col-md-5">
#Html.DisplayNameFor(model => model.Active)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.AutoMake)
</td>
<td>
#Html.DisplayFor(modelItem => item.Active)
</td>
#if (!item.Active)
{
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.MakeID }) |
Activate
</td>
}
else
{
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.MakeID }) |
Deactivate
</td>
}
</tr>
}
</tbody>
</table>
Partial View Table 2
#model IEnumerable<ProjectName.Models.code_Funding>
<h3>Funding List</h3>
<table class="table table table-bordered table-striped">
<tr>
<th>
#Html.DisplayNameFor(model => model.Funding)
</th>
<th>
#Html.DisplayNameFor(model => model.Active)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Funding)
</td>
<td>
#Html.DisplayFor(modelItem => item.Active)
</td>
<td>
#Html.ActionLink("Edit", "Edit", "code_Funding",new { id=item.FundID }, null) |
</td>
</tr>
}
</table>
Partial View 1 Create
#model ProjectName.Models.code_AutoMake
#using (Html.BeginForm("Create", "code_AutoMake", FormMethod.Post))
{
#Html.AntiForgeryToken()
<h3>Add Auto Make</h3>
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="row">
<div class="col-sm-12 col-md-3">
#Html.Label("Auto Make")
#Html.EditorFor(model => model.AutoMake, new {htmlAttributes = new {#class = "form-control"}})
</div>
<div class="col-sm-12 col-md-3">
#Html.Label("Active")
<div class="checkbox">
#Html.EditorFor(model => model.Active)
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-md-3">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Partial View 2 Create
#model ProjectName.Models.code_Funding
#using (Html.BeginForm("Create", "code_Funding", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<h3>Add Funding</h3>
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="row">
<div class="col-sm-12 col-md-3">
#Html.Label("Funding")
#Html.EditorFor(model => model.Funding, new {htmlAttributes = new {#class = "form-control"}})
</div>
<div class="col-sm-12 col-md-3">
#Html.Label("Active")
<div class="checkbox">
#Html.EditorFor(model => model.Active)
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12 col-md-3">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Main View
<div id="AutoMake" class="tab-pane fade active in">
<div id="AutoMake-Index">#{Html.RenderAction("Index", "code_AutoMake");}</div>
<hr/>
#{Html.RenderAction("Create", "code_AutoMake");}
</div>
#*Funding*#
<div id="Funding" class="tab-pane fade">
#{Html.RenderAction("Index", "code_Funding");}
<hr/>
#{Html.RenderAction("Create", "code_Funding");}
</div>
Now here is the scenario.. When I want to create a new autoMake.. I fill out the form and hit submit.. this goes through fine.. until I get back to the Main View.. specifically this line:
#{Html.RenderAction("Create", "code_Funding");}
and I get a runtime error saying:
Child Actions are not allowed to perform redirect actions
I have debugged.. and for some reason.. the HttpPost Create action for code_Funding is being hit.. even when I'm not filling out the create form for code_funding.. How is that possible?
Here are my Create Methods for code_autoMake and code_funding:
code_Funding
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "FundID,Funding,Active")] code_Funding code_Funding)
{
try
{
if (ModelState.IsValid)
{
db.code_Funding.Add(code_Funding);
db.SaveChanges();
return RedirectToAction("EditDDL", "tblNewAutos");
}
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
return RedirectToAction("EditDDL", "tblNewAutos");
}
code_autoMake
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MakeID,AutoMake,Active")] code_AutoMake code_AutoMake)
{
if (ModelState.IsValid)
{
db.code_AutoMake.Add(code_AutoMake);
db.SaveChanges();
return PartialView("~/Views/PartialViews/_AutoMakeCreate.cshtml");
}
return RedirectToAction("EditDDL", "tblNewAutos");
}
Why when I try and create a new automake.. both HttpPost Create methods are hit?
Any help is appreciated.
Well, the problem is following. In your main view you have got this code:
...
#{Html.RenderAction("Create", "code_AutoMake");}
...
Which triggers the Create action which finishes with the following line of code if ModelState.IsValid == false:
return RedirectToAction("EditDDL", "tblNewAutos");
That is obviously a bad idea. Why? You are already in a process of rendering a parent view. Child actions might be a bit confusing at first because they are not real actions - no client/server communication. You are still on the server side. Therefore no redirect is allowed in the child action.
Solutions
First of all, I am not quite sure what you want to achieve so my solution recommendation might be a bit off, but let's see.
Option 1
You may want to use two different actions. One that is called on submit of the form and another one that is called from your main view. The latter one should not make a redirect - instead it should wisely choose which view to render based on the ModelState.IsValid if this is really what you need.
Option 2
There is a hack way which allows you to make redirect from a child action. Instead of making a redirect, only store information about required redirect for instance in HttpContext.Items collection. Then, implement an ActionFilter and in its OnResultExecuted event, check if the redirect request was set to the HttpContext.Items. If so, make a redirect. The ActionFilter should be applied on the parent action, not on the child action.
#{Html.RenderAction("Create", "code_Funding");}
in this RenderAction Method is call the GET request but, your controller you written only post method. and you write the
[ChildActionOnly]
public ActionResult Create(string parm)
{
reurn view()
}
and use [ChildActionOnly] this attribute is allowing restricted access via code in View.
Check and reply me..

Categories