从单个 MVC 页面上的多个部分视图读取用户输入的值

发布于 2024-12-04 20:04:46 字数 12066 浏览 0 评论 0 原文

我正在创建一个新的 MVC v3 Razor Web 应用程序,并且似乎在从部分视图读取值时遇到问题。

我有一个 ViewModel(标题),它由其他三个 ViewModel 组成

    public ProfileVM Profile { get; set; }
    public SpecsVM Specs { get; set; }
    public GeneralVM General { get; set; }
    public Guid Id { get; set; }

我有一个强类型化到 Title 视图模型的页面,在该页面上我使用 @Html.RenderPartial 语法来渲染每个其他 ViewModel(配置文件等) ) 到不同的局部视图 这似乎工作正常,但我希望页面上只有一个按钮来处理数据的发布,并且希望捕获用户输入到部分视图中的值。 这可能吗? 提前致谢

根据表单部分的要求生成 HTML。我还没有添加母版页信息

  <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post"> 
    <fieldset>
        <h1>
            Title Details</h1>
    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">
    <fieldset>
        <p>
            <input type="submit" value="Save" name="TitleGeneral"/>
        </p>
        <hr />
        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="TitleCode">Code</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="TitleCode" name="TitleCode" type="text" value="T00006" /> <span class="field-validation-valid" data-valmsg-for="TitleCode" data-valmsg-replace="true"></span>
                </td>
                <td rowspan="7"> <img src="../../../../Content/demoBF.jpg" width="150px" height="150px" /></td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="TitleName">Title</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="TitleName" name="TitleName" type="text" value="Beaches Of France" />
                    <span class="field-validation-valid" data-valmsg-for="TitleName" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="SubTitle">Sub Title</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="SubTitle" name="SubTitle" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="SubTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="SortingTitle">Sorting Title (If Different)</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="SortingTitle" name="SortingTitle" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="SortingTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="ShortTitle">Short or abbreviated title</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="ShortTitle" name="ShortTitle" type="text" value="TRO1" />
                    <span class="field-validation-valid" data-valmsg-for="ShortTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Comments">Comments</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="Comments" name="Comments" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Comments" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Author">Author</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="Author" name="Author" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Author" data-valmsg-replace="true"></span>
                </td>

         </tr>
        </table>
    </fieldset>
    </form>        <br />
        <br />
       <ul class="tabs">
            <li><a href="#tab1">Profile</a></li>
            <li><a href="#tab2">Specification</a></li>
            <li><a href="#tab3">Authors</a></li>
            <li><a href="#tab4">Editions</a></li>
            <li><a href="#tab5">Restrictions</a></li>
             <li><a href="#tab6">Licences</a></li>
              <li><a href="#tab7">Purchases</a></li>
               <li><a href="#tab8">Restrictions</a></li>

        </ul>
        <div class="tab_container">
            <div id="tab1" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">         
    <fieldset>

         <p>
            <input type="submit" value="Save" name="TitleProfile"/>
        </p>


    </fieldset>
    </form>            </div>
            <div id="tab2" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">        
    <fieldset>


        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="Pages">Pages</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Pages must be a number." id="Pages" name="Pages" type="text" value="96" />
                    <span class="field-validation-valid" data-valmsg-for="Pages" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Plates">Plates or illustrations</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Plates or illustrations must be a number." id="Plates" name="Plates" type="text" value="64" />
                    <span class="field-validation-valid" data-valmsg-for="Plates" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="PlateType">Plate Type</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="PlateType" name="PlateType" type="text" value="Full-colour photographs" />
                    <span class="field-validation-valid" data-valmsg-for="PlateType" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Format">Format</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="Format" name="Format" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Format" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="OtherSpecs">Other specifications</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="OtherSpecs" name="OtherSpecs" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="OtherSpecs" data-valmsg-replace="true"></span>
                </td>
            </tr>
        </table>

    </fieldset>
    </form>            </div>
            <div id="tab3" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">        
    <fieldset>


        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="Pages">Pages</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Pages must be a number." id="Pages" name="Pages" type="text" value="96" />
                    <span class="field-validation-valid" data-valmsg-for="Pages" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Plates">Plates or illustrations</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Plates or illustrations must be a number." id="Plates" name="Plates" type="text" value="64" />
                    <span class="field-validation-valid" data-valmsg-for="Plates" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="PlateType">Plate Type</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="PlateType" name="PlateType" type="text" value="Full-colour photographs" />
                    <span class="field-validation-valid" data-valmsg-for="PlateType" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Format">Format</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="Format" name="Format" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Format" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="OtherSpecs">Other specifications</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="OtherSpecs" name="OtherSpecs" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="OtherSpecs" data-valmsg-replace="true"></span>
                </td>
            </tr>
        </table>

    </fieldset>
</form>            </div>
        </div>
    </fieldset>
</form><div>
    <a href="/Titles/Title">Back to List</a>
</div>
</td></tr>

        </table>

I am in the process of creating a new MVC v3 Razor web application and seem to be having issues in reading values from partial views.

I have a ViewModel (title) which is made up of three other ViewModels

    public ProfileVM Profile { get; set; }
    public SpecsVM Specs { get; set; }
    public GeneralVM General { get; set; }
    public Guid Id { get; set; }

I have a page which is strongly typed to the Title view model, on which I am using the @Html.RenderPartial syntax to render each of the other ViewModels (profile etc) to different partial view
This appears to be working correctly, but I would like only one button on the page to handle the posting of data, and want to capture the values entered by the user into the partial views.
Is this possible?
Thanks in advance

Generated HTML as requested of the form sections. I've not added the master page information

  <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post"> 
    <fieldset>
        <h1>
            Title Details</h1>
    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">
    <fieldset>
        <p>
            <input type="submit" value="Save" name="TitleGeneral"/>
        </p>
        <hr />
        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="TitleCode">Code</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="TitleCode" name="TitleCode" type="text" value="T00006" /> <span class="field-validation-valid" data-valmsg-for="TitleCode" data-valmsg-replace="true"></span>
                </td>
                <td rowspan="7"> <img src="../../../../Content/demoBF.jpg" width="150px" height="150px" /></td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="TitleName">Title</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="TitleName" name="TitleName" type="text" value="Beaches Of France" />
                    <span class="field-validation-valid" data-valmsg-for="TitleName" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="SubTitle">Sub Title</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="SubTitle" name="SubTitle" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="SubTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="SortingTitle">Sorting Title (If Different)</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="SortingTitle" name="SortingTitle" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="SortingTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="ShortTitle">Short or abbreviated title</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="ShortTitle" name="ShortTitle" type="text" value="TRO1" />
                    <span class="field-validation-valid" data-valmsg-for="ShortTitle" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Comments">Comments</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="Comments" name="Comments" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Comments" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Author">Author</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="Author" name="Author" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Author" data-valmsg-replace="true"></span>
                </td>

         </tr>
        </table>
    </fieldset>
    </form>        <br />
        <br />
       <ul class="tabs">
            <li><a href="#tab1">Profile</a></li>
            <li><a href="#tab2">Specification</a></li>
            <li><a href="#tab3">Authors</a></li>
            <li><a href="#tab4">Editions</a></li>
            <li><a href="#tab5">Restrictions</a></li>
             <li><a href="#tab6">Licences</a></li>
              <li><a href="#tab7">Purchases</a></li>
               <li><a href="#tab8">Restrictions</a></li>

        </ul>
        <div class="tab_container">
            <div id="tab1" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">         
    <fieldset>

         <p>
            <input type="submit" value="Save" name="TitleProfile"/>
        </p>


    </fieldset>
    </form>            </div>
            <div id="tab2" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">        
    <fieldset>


        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="Pages">Pages</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Pages must be a number." id="Pages" name="Pages" type="text" value="96" />
                    <span class="field-validation-valid" data-valmsg-for="Pages" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Plates">Plates or illustrations</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Plates or illustrations must be a number." id="Plates" name="Plates" type="text" value="64" />
                    <span class="field-validation-valid" data-valmsg-for="Plates" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="PlateType">Plate Type</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="PlateType" name="PlateType" type="text" value="Full-colour photographs" />
                    <span class="field-validation-valid" data-valmsg-for="PlateType" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Format">Format</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="Format" name="Format" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Format" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="OtherSpecs">Other specifications</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="OtherSpecs" name="OtherSpecs" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="OtherSpecs" data-valmsg-replace="true"></span>
                </td>
            </tr>
        </table>

    </fieldset>
    </form>            </div>
            <div id="tab3" class="tab_content">
                <!--Content-->

    <form action="/Titles/Title/Edit/2bb75b02-9dd7-e011-971f-0003fffb8797" method="post">        
    <fieldset>


        <table>
            <tr>
                <td class="editor-label" style="width: 150px;"><label for="Pages">Pages</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Pages must be a number." id="Pages" name="Pages" type="text" value="96" />
                    <span class="field-validation-valid" data-valmsg-for="Pages" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label"><label for="Plates">Plates or illustrations</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" data-val="true" data-val-number="The field Plates or illustrations must be a number." id="Plates" name="Plates" type="text" value="64" />
                    <span class="field-validation-valid" data-valmsg-for="Plates" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="PlateType">Plate Type</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="PlateType" name="PlateType" type="text" value="Full-colour photographs" />
                    <span class="field-validation-valid" data-valmsg-for="PlateType" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="Format">Format</label>
                </td>
                <td class="editor-label"><input class="text-box single-line" id="Format" name="Format" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="Format" data-valmsg-replace="true"></span>
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    <label for="OtherSpecs">Other specifications</label>
                </td>
                <td class="editor-label">
                    <input class="text-box single-line" id="OtherSpecs" name="OtherSpecs" type="text" value="" />
                    <span class="field-validation-valid" data-valmsg-for="OtherSpecs" data-valmsg-replace="true"></span>
                </td>
            </tr>
        </table>

    </fieldset>
</form>            </div>
        </div>
    </fieldset>
</form><div>
    <a href="/Titles/Title">Back to List</a>
</div>
</td></tr>

        </table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-12-11 20:04:46

问题是输入字段的名称不正确。它们缺少指示主要父属性名称的前缀。这是由于使用了部分而不是编辑器模板。吉米·博加德在这个 博客文章

因此,不要

@Html.Partial("_somePartial", Model.Profile)

使用:

@Html.EditorFor(x => x.Profile)

,框架将负责正确命名字段。

The problem is that the names of the input fields are incorrect. They are missing the prefix indicating the main parent property name. That's due to trhe usage of partials instead of editor templates. Jimmy Bogard explained this perfectly in this blog post.

So instead of:

@Html.Partial("_somePartial", Model.Profile)

use:

@Html.EditorFor(x => x.Profile)

and the framework will take care of properly naming the fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文