如何将视图从视图传递到控制器

时间:2021-11-17 18:07:51

I have problem when pass a list in an object from view to controller, the list is always null. How should I do this?

将对象中的列表从视图传递到控制器时遇到问题,列表始终为null。我该怎么做?

My model:

我的模特:

public class ImageModel
{
    public int id { get; set; }
    public string url { get; set; }
    public bool delete { get; set; }
}

public class SchoolImages
{
    public Nullable<int> schoolid { get; set; }
    public IList<ImageModel> images;
}

The view:

风景:

@model SchoolAppManager.Models.SchoolImages

@using (Html.BeginForm("DeleteImages", "Images"))
{

    @Html.HiddenFor(x => x.schoolid)
    <table>
    @for (int i = 0; i < Model.images.Count(); i += 4) {

        <tr>
            <td>
                <img src="@Url.Content(Model.images[i].url)" width="50%" />
                <br />
                @Html.CheckBoxFor(x => x.images[i].delete)
                @Html.HiddenFor(x => x.images[i].id)
            </td>
            <td>
                @if (i + 1 < Model.images.Count())
                {
                    <img src="@Url.Content(Model.images[i + 1].url)" width="50%" />
                    <br />
                    @Html.CheckBoxFor(x => x.images[i + 1].delete)
                    @Html.HiddenFor(x => x.images[i + 1].id)
                }
            </td>
            <td>
                @if (i + 2 < Model.images.Count())
                {
                    <img src="@Url.Content(Model.images[i + 2].url)" width="50%" />
                    <br />
                    @Html.CheckBoxFor(x => x.images[i + 2].delete)
                    @Html.HiddenFor(x => x.images[i + 2].id)
                }
            </td>
            <td>
                @if (i + 3 < Model.images.Count())
                {
                    <img src="@Url.Content(Model.images[i + 3].url)" width="50%" />
                    <br />
                    @Html.CheckBoxFor(x => x.images[i + 3].delete)
                    @Html.HiddenFor(x => x.images[i + 3].id)
                }
            </td>
        </tr>
    }
    </table>

    <input type="submit" value="delete" />
}

When I click delete, SchoolImages is passed to the controller, SchoolImages.schoolId has value in the controller, however, SchoolImages.images is null. How can I pass SchoolImages.images to controller?

当我单击删除时,SchoolImages将传递给控制器​​,SchoolImages.schoolId在控制器中具有值,但是,SchoolImages.images为null。如何将SchoolImages.images传递给控制器​​?

4 个解决方案

#1


6  

Default model binder doesn't work with fields, so make images a property:

默认模型绑定器不适用于字段,因此将图像作为属性:

public IList<ImageModel> images { get; set; }

#2


1  

I guess you will have problem binding complex types to your model. In case you don't get that working. Here is the alternative way.

我想你会遇到将复杂类型绑定到模型的问题。如果你没有那个工作。这是另一种方式。

You have a bunch of checkboxes, possibly with the same name and different values. You could post them to a method that takes a FormCollection, ie.

你有一堆复选框,可能有相同的名称和不同的值。您可以将它们发布到采用FormCollection的方法,即。

public ActionResult Test(FormCollection collection)
{
    string results = collection["Blanks"];
}

This would give you a comma-delimited list of values (or null, where no checkboxes are ticked).

这将为您提供逗号分隔的值列表(或null,其中没有勾选复选框)。

Alternatively, if you have the possible values as an array on the server then you could give the checkboxes names according to their array values, which would mean you could do something like this:

或者,如果您在服务器上将可能的值作为数组,则可以根据其数组值给出复选框名称,这意味着您可以执行以下操作:

@using (Html.BeginForm("Test","Home"))
{
    @Html.CheckBox("Blanks[0]", false);
    @Html.CheckBox("Blanks[1]", false);
    @Html.CheckBox("Blanks[2]", false);

    <input type="submit" value="Submit" />
}

giving you an array of booleans in your Test method:

在Test方法中为您提供一系列布尔值:

public ActionResult Test(bool[] Blanks) { }

公共ActionResult测试(bool []空白){}

#3


0  

Try changing your model "SchoolImages"; use array instead of IList<>.

尝试更改您的模型“SchoolImages”;使用数组而不是IList <>。

#4


0  

It's null because the default model binder doesn't know how to initialize it. Change your field images to be a property and add a constructor like this:

它为null,因为默认模型绑定器不知道如何初始化它。将您的字段图像更改为属性并添加如下构造函数:

public class SchoolImages
{
    public SchoolImages()
    {
        images = new List<ImageModel>();
    }
    public Nullable<int> schoolid { get; set; }
    public IList<ImageModel> images { get; set; }
}

It should work.

它应该工作。

#1


6  

Default model binder doesn't work with fields, so make images a property:

默认模型绑定器不适用于字段,因此将图像作为属性:

public IList<ImageModel> images { get; set; }

#2


1  

I guess you will have problem binding complex types to your model. In case you don't get that working. Here is the alternative way.

我想你会遇到将复杂类型绑定到模型的问题。如果你没有那个工作。这是另一种方式。

You have a bunch of checkboxes, possibly with the same name and different values. You could post them to a method that takes a FormCollection, ie.

你有一堆复选框,可能有相同的名称和不同的值。您可以将它们发布到采用FormCollection的方法,即。

public ActionResult Test(FormCollection collection)
{
    string results = collection["Blanks"];
}

This would give you a comma-delimited list of values (or null, where no checkboxes are ticked).

这将为您提供逗号分隔的值列表(或null,其中没有勾选复选框)。

Alternatively, if you have the possible values as an array on the server then you could give the checkboxes names according to their array values, which would mean you could do something like this:

或者,如果您在服务器上将可能的值作为数组,则可以根据其数组值给出复选框名称,这意味着您可以执行以下操作:

@using (Html.BeginForm("Test","Home"))
{
    @Html.CheckBox("Blanks[0]", false);
    @Html.CheckBox("Blanks[1]", false);
    @Html.CheckBox("Blanks[2]", false);

    <input type="submit" value="Submit" />
}

giving you an array of booleans in your Test method:

在Test方法中为您提供一系列布尔值:

public ActionResult Test(bool[] Blanks) { }

公共ActionResult测试(bool []空白){}

#3


0  

Try changing your model "SchoolImages"; use array instead of IList<>.

尝试更改您的模型“SchoolImages”;使用数组而不是IList <>。

#4


0  

It's null because the default model binder doesn't know how to initialize it. Change your field images to be a property and add a constructor like this:

它为null,因为默认模型绑定器不知道如何初始化它。将您的字段图像更改为属性并添加如下构造函数:

public class SchoolImages
{
    public SchoolImages()
    {
        images = new List<ImageModel>();
    }
    public Nullable<int> schoolid { get; set; }
    public IList<ImageModel> images { get; set; }
}

It should work.

它应该工作。