【问题标题】:Is this a valid model for such a view?对于这种观点,这是一个有效的模型吗?
【发布时间】:2023-04-02 03:31:01
【问题描述】:

它是正确的模型吗?

public class NewForm
{
    public string[] Field { get; set; }
    public bool[] Check { get; set; }
}

对于这样的视图:

@Html.TextAreaFor(model => model.Field)
@Html.TextAreaFor(model => model.Field)
@Html.TextAreaFor(model => model.Field)

@Html.CheckBoxFor(model => model.Check)
@Html.CheckBoxFor(model => model.Check)

或者有没有更好的方法来创建同名字段? 在 Controller 中仅显示第一个值。但我需要所有的

        [HttpPost]

    public ActionResult Edit(NewForm model)
    {
        Response.Write(model.Field);
        Response.Write(model.Check);
    }

字段可能是不确定的数字,因为通过单击按钮 JavaScript 会添加一个具有相同名称的新字段

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

听起来就像您想将model 的多个实例提交回控制器。

你可以做这样的事情。我的示例会将 10 个 Field 实例提交回您的控制器。

查看:

@using (Html.BeginForm())
{
    <div>
    @for(int i = 0; i<10; i++) 
    {
        <div>@Html.TextBox("items[" + i + "].Field", "", new { id = "items[" + i + "].Field", placeholder = "Enter Text..." })</div>
        @Html.Hidden("items.Index", i)
    }
    </div>
    <input type="submit" value="Submit" />
}

类:

public class MyClass 
{
    public string Field {get;set;}
}

控制器方法:

[HttpPost]
public ActionResult ActionName(List<MyClass> items)
{
   //...do stuff
}

显然,您也可以将复选框添加到模型和表单中,以便提交其中的许多内容。

【讨论】:

    【解决方案2】:

    为什么要为字段同名,每个字段都有正确的名称

    public class NewForm
    {
        public string FirstField { get; set; }
        public string Field { get; set; }
        public bool Check { get; set; }
    }
    

    查看

    @Html.TextAreaFor(model => model.FirstField)
    @Html.TextAreaFor(model => model.Field)
    
    @Html.CheckBoxFor(model => model.Check)
    

    ee

    [HttpPost]
    public ActionResult Edit(NewForm model)
    {
        Response.Write(model.FirstField);
        Response.Write(model.Field);
        Response.Write(model.Check);
    }
    

    【讨论】:

    • 如果有多个相同名称的字段,如何在控制器上接收数据,是数组吗?
    【解决方案3】:

    不,不是。在您定义的模型中,您创建了两个不同的数组:第一个属性Field 是一个字符串数组,第二个属性Check 是一个布尔数组。将[] 放在类型表示数组之后。

    如果你有一个未知数量的我称之为“迷你表单”,并且这些数量是由用户通过 UI 决定的,那么你应该创建一个视图模型来表示这个迷你表单,并创建一个容器视图模型来容纳它以及您的视图需要的任何其他属性。

    例如:

    public class MiniFormViewModel
    {
        public string MyInput { get; set; }
        public bool MyCheck { get; set; }
    }
    

    然后在您的容器视图模型中:

    public class ContainerViewModel
    { 
        public IEnumerable<MiniFormViewModel> MiniForms { get; set; } 
        //Any other properties you need on the view that will occur a single time 
    }
    

    现在,您需要在 JS 中添加一些操作才能做到这一点:

    function getViewModel() {
        //You'll have to decide how you want to get the values of the mini form's fields. Perhaps you might even have a function to supply these values. Up to you.
        return {
            MiniForms: [{
                    MyInput: '', //value from the first "mini form' string field
                    Mycheck: false //value from the first "mini-form" bool field
                },
                {
                    MyInput: '', //value from the second"mini form' string field
                    Mycheck: false //value from the second"mini-form" bool field
                }      
            ]
        }
    }
    

    然后您需要将其发布回服务器。我将通过内置的 JS Fetch 函数演示如何做到这一点:

            fetch(yourUrlForTheControllerAction,
                {
                    method: 'post',
                    body: JSON.stringify(getViewModel()),
                    headers: {
                        'content-type': 'application/json; charset=UTF-8'
                    }
                })
    

    然后是 blammo,你应该很高兴。我排除了动态添加迷你表单字段的部分,因为听起来您已经有了解决方案。

    【讨论】:

    • 由于点击按钮JavaScript会添加一个同名的新字段,因此可能存在不定数量的字段
    • 啊,我明白了。然后你必须在 JS 中做一些操作来创建一个字符串数组和 bool,JSON.stringify 并将其发布回模型。据我所知,没有 Razor 助手可以执行您所描述的操作。
    • @vamp123 我对原始答案进行了编辑。看看这对你来说是不是一个可行的解决方案!如果还有什么我可以提供的帮助您,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2014-03-25
    • 2016-11-24
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多