'How to get and Bind() data from dynamic view in asp.net
I wrote Create() method to create a Test and I input that data- 1)choose from dropdownlist ,for which Course I want to add this Test 2)name 3)description I have issue when try to use method asp-for for dynamic model i do not know how use it correctly .For example, If i use Create(Bind("name, description,...")Test test) and in View =>asp-for="name"....asp-for="description" -> i get msg that i can not use asp-for dynamic
This is my Create() methods in TestController.cs
public IActionResult Create()
{
dynamic mymodel = new ExpandoObject();
mymodel.test = new Test();
mymodel.coursesChoose = courseService.GetAllCourses().ToList();
return View(mymodel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([Bind("name,description,CourseId,Course")] Test test)
{
//testService.AddNewTest(test);
Console.WriteLine("///////////" + test.name + test.description + test.CourseId + test.Course.name);//returns null
return RedirectToAction(nameof(GetAllTests));
}
This is mu cshtml View
@{
ViewData["Title"] = "Create";
Layout = "_Layout";
}
<div class="mask d-flex align-items-center h-100 gradient-custom-3">
<div class="container h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-12 col-md-9 col-lg-7 col-xl-6">
<div class="CreateCourseForm card" style="border-radius: 15px;">
<div class="card-body p-5">
<h2 class="text-uppercase text-center text-white mb-4">Create test</h2>
<form method="post" asp-controller="Test" asp-action="Create">
@Html.AntiForgeryToken()
<div class="form-outline mb-4">
<label class="form-label text-white" for="form3Example1cg">Course Name</label>
<div class="form-outline mb-4">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>Choose Course...</option>
@foreach(Course course in Model.coursesChoose)
{
<option CourseId="@course.id" Course ="@course" >@course.name</option>
}
</select>
</div>
</div>
<div class="form-outline mb-4">
<label class="form-label text-white" for="form3Example1cg">Test Name</label>
<input type="text" id="form3Example1cg" class="form-control form-control-lg" asp-for"name" value="@testMod.name"/>
</div>
<div class="form-group">
<label class="form-label text-white" for="exampleFormControlTextarea1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="4" asp-for"description" value="@testMod.description"></textarea>
</div>
<div class="d-flex justify-content-end">
<a class="CancelButton text-white" asp-area="" asp-controller="Test" asp-action="GetAllTests">Cancel</a>
<input type="submit" class="SaveCourseButton btn text-white" name="save" value="Save" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Solution 1:[1]
I solved my problem by not using dynamic model. I passed my available Courses using ViewData("myAviList") = courseRepository.GetAllCourses()
And get it in cshtml
file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | siddharth |