从使用模型到另一个模型的视图中,数据不会进入模型 [英] Data is not coming in model from the view using model into another model

查看:67
本文介绍了从使用模型到另一个模型的视图中,数据不会进入模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用给定模型将数据和列表数据添加到单个视图中,数据列表工作正常,但在添加数据时,没有模型正常工作。



我有两个模型AddStudent和ListStudent,我通过合并到另一个模型AddStudentList在视图中使用它们。



I am trying to add data and list data into a single view using given models the data listing is working fine but while adding data no models is working.

I have two models AddStudent and ListStudent and I am using both of them in a view by merging into another model AddStudentList.

public class AddStudent
{
    public int EnrollmentNo { get; set; }
    public string StudentName { get; set; }
}

public class ListStudent
{
    public int EnrollmentNo { get; set; }
    public string StudentName { get; set; }
}

public class AddStudentList
{
    public AddStudent addStudentData {get; set;}
    public List<ListStudent> listStudentData {get; set;}
}





StudentController中的代码我正在使用:





The code in StudentController that i am using:

AddStudent addstudent = new AddStudent();
AddStudentList asl = new AddStudentList();
addStudent = asl.AddStudentData;
TryUpdateModel(addStudent);





这是我对这些模型的看法:



This is my view for these models:

@model AddStudentList

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Add Student</h1>
<hr>
<div class="row row-centered">
    <div class="col-md-4 col-md-offset-3">


        @using (Html.BeginForm("AddStudent", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="form-group">
                @Html.TextBoxFor(m => m.AddStudentData.EnrollmentNo, new { @class = "form-control animated", placeholder = "Category Name", tabindex = 1 })
                @Html.ValidationMessageFor(m => m.AddStudentData.EnrollmentNo, null, new { @class = "error-class animated", @Style = "color:red;" })
            </div>
            <div class="form-group">
                @Html.DropDownListFor(m => m.AddStudentData.StudentName, new { @class = "form-control animated", tabindex = 2 })
                @Html.ValidationMessageFor(m => m.AddStudentData.StudentName, null, new { @class = "error-class animated", @Style = "color:red;" })
            </div>
            <div class="form-group">
                <div class="row">
                    <div class="col-sm-6 col-sm-offset-3">
                        <input type="submit" name="CategorySubmit" id="CategorySubmit" tabindex="6" class="form-control btn btn-login" value="Add Category">
                    </div>
                </div>
            </div>
        }
    </div>
</div>

    @{
        var grid = new WebGrid(Model.ListStudentData, canPage: true, rowsPerPage: 50);
    }
        @if (grid.Rows.Count > 0)
        {
            @grid.GetHtml(
                          htmlAttributes: new { @class = "table table-bordered table-responsive" },
                         columns:
                         grid.Columns(

                         grid.Column("EnrollmentNo", "Enrollment No."),

                         grid.Column("StudentName", "Student Name")))
}





我是什么尝试过:



我尝试过单独使用子模型和父模型,但数据仍未从视图传递到所需模型。



What I have tried:

I have tried using individually child and parent model but still data is not passing from view to the required model.

推荐答案

尝试使用模型的编辑器模板。

如果可能请将cshtml视图代码添加到您的问题中。

请点击以下链接

c# - 复杂模型和部分视图 - ASP.NET MVC 3中的模型绑定问题 [ ^ ]



谢谢
Try using the editor template for the model.
If possible please add the cshtml view code onto your question.
Please follow the below link
c# - Complex models and partial views - model binding issue in ASP.NET MVC 3[^]

Thanks


问题在于控制器从模型中获取数据,这里是我使用过的代码并且它有效。我只是使用父模型AddStudentList并使用AddStudentList.AddStudent将值传递给数据库,它工作正常。



上一个代码:

The problem is in controller to get data from the model and here is the code which I have used and it worked. I just use the parent model AddStudentList and pass the values to the database using AddStudentList.AddStudent and it worked fine.

Previous Code:
AddStudent addstudent = new AddStudent();
AddStudentList asl = new AddStudentList();
addStudent = asl.AddStudentData;
TryUpdateModel(addStudent);





替换为此代码:



Replaced with this code:

AddStudentList asl = new AddStudentList();
TryUpdateModel(asl);


这篇关于从使用模型到另一个模型的视图中,数据不会进入模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆