【问题标题】:Passing data from a View to a Controller in .NET MVC - "@model" not highlighting在 .NET MVC 中将数据从视图传递到控制器 - “@model”未突出显示
【发布时间】:2017-12-02 00:00:51
【问题描述】:

以下代码可以按我的需要工作:

 @using (Html.BeginForm("LOLOL", "PATIENT", null))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>PATIENT</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>

        </fieldset>
        <p>
            <input type="submit" value="SUBMIT" />
        </p>
    }  

在 LOLOLController 中:

[HttpPost]
    public IActionResult LOLOL(Patient p) {
        var client = new MongoClient("mongodb://localhost:27017");
        var userId = _userManager.GetUserId(HttpContext.User);
        string db_name = "test" + userId;
        var database = client.GetDatabase(db_name);
        var collection = database.GetCollection<BsonDocument>("patients");
        var filter = Builders<BsonDocument>.Filter.Eq("Name", p.Name.ToString());
        var document = collection.Find(filter).First();
        // I'm cutting short the rest of the code, because when I do something 
 // similar later, "collection.Find(filter).First()" fires an exception, I'll     
// explain..


        return View(p);
    }

我有一些相当于去掉 HTML 中的 fieldset 元素,基本上只在“Html.BeginForm”中留下一个按钮,但是数据显然没有正确绑定,我知道这是因为如果我只有一个按钮并且没有数据输入,我单击按钮,然后我收到一条错误消息,指出无法从数据库中找到数据。 (编辑:我现在已经确认这确实是因为 Patient 对象没有像我预期的那样传递给控制器​​,似乎是在调用 html.beginform 时创建了一个全新的 Patient 对象......我想也许旧的 Patient 对象正在被传递,所以每次我们使用 Html.BeginForm 时我都不必输入它的所有数据成员)

总而言之,我想填写一个文本框,单击一个按钮以加载新页面并显示该文本框的值,但该值也基本上保持在会话状态中,所以如果我调用另一个 Html.BeginForm 函数并进入第三个视图,第一个视图中的文本将显示在第三个视图中,即使我不必在第二个视图中键入它的值。 希望我可以重复这个过程,本质上是加载一个类的数据成员,每个数据成员有一个视图。

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    确保将数据从前一个视图传递到控制器的新视图。当您传递它时,将 @HiddenFor 包含在新视图中的前一个视图中的那些属性。这样,新视图将保留,然后将值传递给您的下一个 POST。

    @Html.HiddenFor(model =&gt; model.PropertyYouPassedAndWantToKeepAndPassAgain

    编辑:这里是为一个对象使用多个视图的逻辑......根据要求。

    型号:

    public class Patient
    {
        string Name { get; set; }
        string Address { get; set; }
        string City { get; set; }
    }
    

    第 1 页获取:

    [HttpGet]
    public ActionResult Page1()
    {
        Patient patient = new Patient();
        return View("~/Views/Page1.cshtml", patient);
    }
    

    第 1 页视图...仅询问名称。

    @model mysite.Models.Patient
    
    @using (Html.BeginForm("LOLOL", "PATIENT", null))
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>PATIENT</legend>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Name)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
    
            </fieldset>
            <p>
                <input type="submit" value="SUBMIT" />
            </p>
        }
    

    Page1 POST...获取患者并将其传递到下一个视图...

    [HttpPost]
    public ActionResult Page1(Patient patient)
    {
        if (ModelState.IsValid)
        {
            return View("~/Views/Page2.cshtml", patient); // pass your patient to the second page view with the name
        }
        else
        {
            return View("~/Views/Page1.cshtml", patient);
        }
    }
    

    Page2 GET... 从之前的 Page1 POST 中获取患者并将其发送到 Page2 视图。

    [HttpGet]
    public ActionResult Page2(Patient patient)
        {
            // Receive patient from Page1 post and pass it to new view... includes the name
            return View("~/Views/Page2.cshtml", patient);
        }
    

    Page2 视图获取对象...使用 HiddenFor 保留您刚刚从 GET 发送的名称。

    @model mysite.Models.Patient
    
    @using (Html.BeginForm("LOLOL", "PATIENT", null))
    {
        @Html.HiddenFor(model => model.Name) @* This will keep the name on your next post *@
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>PATIENT</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Address)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Address)
                @Html.ValidationMessageFor(model => model.Address)
            </div>
    
        </fieldset>
        <p>
            <input type="submit" value="SUBMIT" />
        </p>
    }
    

    由于 HiddenFor 拥有名称,它将在您的下一篇文章中传递。它在那里,但对表单本身隐藏。

    [HttpPost]
    public ActionResult Page2(Patient patient)
    {
        // Because of the HiddenFor, the Name will be passed because it was kept in the view... but hidden from the form itself.
        // It's basically storing it for you to pass again
        if (ModelState.IsValid)
        {
            // Pass object with Name and Address to next controller
            return View("~/Views/Page3.cshtml", patient);
        }
        else
        {
            return View("~/Views/Page2.cshtml", patient);
        }
    }
    

    第2页发布

    [HttpPost]
    public ActionResult Page2(Patient patient)
    {
        // Because of the HiddenFor, the Name will be passed because it was kept in the view... but hidden from the form itself.
        // It's basically storing it for you to pass again
        if (ModelState.IsValid)
        {
            // Pass object with Name and Address to next controller
            return View("~/Views/Page3.cshtml", patient);
        }
        else
        {
            return View("~/Views/Page2.cshtml", patient);
        }
    }
    

    第3页获取

    [HttpGet]
    public ActionResult Page3(Patient patient)
    {
        // Pass patient again... to your next view
        return View("~/Views/Page3.cshtml", patient);
    }
    

    第 3 页视图...

    @using (Html.BeginForm("LOLOL", "PATIENT", null))
    {
        @Html.HiddenFor(model => model.Name) @* Keep name again for your next post *@
        @Html.HiddenFor(model => model.Address) @* Now we are keeping the address as well *@
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>PATIENT</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.City)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.City)
                @Html.ValidationMessageFor(model => model.City)
            </div>
    
        </fieldset>
        <p>
            <input type="submit" value="SUBMIT" />
        </p>
    } 
    

    依此类推……直到你的模型完成并想用它做点什么。

    【讨论】:

    • 刚刚编辑了我的答案,因为您似乎知道自己在做什么...我猜这可能是编译器没有接收到让我失去无数小时的更改。如果您复制视图并对其进行更改或重命名,有时会发生这种情况。 Visual Studio 中的错误列表说明了什么?
    • 您是在每个视图中使用不同的控制器还是相同的控制器?我明白你现在想要达到的目标。您可以发布 GET 吗?编辑了我的帖子。如果这样做,请告诉我。
    • 是的。您想使用 @HiddenFor 来保留不在表单上的模型属性...我将尝试解释您想要更好地遵循的过程。
    • 我会为每个视图设置不同的控制器,因为您必须从每个 POST 返回一个新视图。
    • 太棒了,问题解决了!谢谢 :) 喜欢这个网站!
    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多