0

Passing department and title models for use data in selectbox and passing employee model for save data from user. tring to pass values from partial view but in controller values return null.

partial view:

@model (List<Department> Departments, List<Title> Titles, Employee e)

    <form class="g-3" asp-action="CreateEmployee" asp-controller="Employees" method="post">
        <div class="row">
            <div class="col-lg-6">
                <div class="mb-3">
                    <label for="Name" class="form-label">İsim</label>
                    <input asp-for="e.Name" type="text" class="form-control" id="Name">
                    <div class="invalid-feedback">
                        İsim alanı boş bırakılamaz.
                    </div>
                </div>
            </div>
        </div>
        <button type="submit">Tek Form</button>
    </form>

controller:

public IActionResult CreateEmployee()
        {
            HR_ManagementContext context = new HR_ManagementContext();
            var departments = context.Departments.ToList();
            var titles = context.Titles.ToList();

            var models = (departments, titles, new Employee());

            return View(models);
        }
 [HttpPost]
        public IActionResult CreateEmployee(Employee employee)
        {

            return RedirectToAction("CreateEmployee");
        }

2 Answers 2

1

Set the name attribute in the input tag:

<input asp-for="e.Name" type="text" class="form-control" id="Name", name="employee.Name">

The second solution is to use model name item3 generated by the MVC:

[HttpPost]
public IActionResult CreateEmployee(Employee item3)
{
    return RedirectToAction("CreateEmployee");
}
Sign up to request clarification or add additional context in comments.

2 Comments

thx, its worked! but i have a question. stackoverflow.com/questions/64133766/… i saw this and its not recomment using name and id with asp-for (because its override) so in this example whats the point of using name? why asp-for not working?
@Selcukusu: Right, but because of you are using the tuple type the model element names will be Item1, Item2 and Item3. And this will prevent from the MVC parser to work properly. Therefore, the best solution is to use the strongly typed view instead of the tuple type.
0

Thx @Jackdaw his answer also working too.

I found an alternative

in Controller you can bind model:

public IActionResult CreateEmployee([Bind(Prefix = "Item3")]Employee employee)
{
    return RedirectToAction("CreateEmployee");
}

Item3 is the prefix of tuple model.

@model (List<Department> Departments, List<Title> Titles, Employee e)
  • Department = Item1
  • Titles = Item2
  • Employee = Item3

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.