MVC2:正在渲染 List可以在创建上下文中吗?

发布于 2024-11-07 17:08:14 字数 929 浏览 0 评论 0原文

我有这个模型:

public class Package
{
    public string CustomerName { get; set; }
    public List<Product> Products { get; set; }
}

public class Product
{
    public int Quantity { get; set; }
    public string Name { get; set; }
}

当我添加创建视图时,代码是:

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.CustomerName) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.CustomerName) %>
            <%: Html.ValidationMessageFor(model => model.CustomerName) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

如何管理产品列表? 我可以获得一个按钮或其他东西来创建新产品并将其添加到产品列表中吗?

谢谢

I have this Model:

public class Package
{
    public string CustomerName { get; set; }
    public List<Product> Products { get; set; }
}

public class Product
{
    public int Quantity { get; set; }
    public string Name { get; set; }
}

When I add the Create's view, the code is:

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.CustomerName) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.CustomerName) %>
            <%: Html.ValidationMessageFor(model => model.CustomerName) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

How manage the Products list?
Can I get a button or something to create a new product and add it to the Products list?

Thank you

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

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

发布评论

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

评论(2

迷路的信 2024-11-14 17:08:14

要创建按钮,您还可以使用 HTML.ActionLink() 或 Ajax.ActionLink() 定义为按钮,例如:

<% Response.Write(Html.ActionLink("Add Product", "Create", new { id = tId, tNum = tNum }, new { @class = "oldVal" })); %>

它将在您的页面上创建一个按钮,在这里您可以看到不同的属性(因此只需检查它们即可)会发现它很有用)...并且从这两个方面来看,使用哪个更多取决于您想要的操作...

并且在您的控制器中:

public ActionResult Create() 
{
    // do same as mentioned by [gnome][1]
}

这里是示例:模式弹出窗口,声明为页面的部分视图
[使用 Ajax.ActionLink() 的部分视图]

using (Ajax.BeginForm("Login", "Users", null, new AjaxOptions() { UpdateTargetId = "divLoginPopupContent" }))
            {
                Response.Write(Html.ValidationSummary(true));
    %>
                <ul class="chooseQuestion">
                    <li>
                        <div class="short">
                            <%= Html.LabelFor(model => model.LoginEmail)%>
                        </div>
                        <div class="editor-field">
                            <%= Html.TextBoxFor(model => model.LoginEmail)%>
                            <%= Html.ValidationMessageFor(model => model.LoginEmail)%>
                        </div>
                    </li><li>
                        <div class="short">
                            <%= Html.LabelFor(model => model.LoginPassword)%>
                        </div>
                        <div class="editor-field">
                            <%= Html.PasswordFor(model => model.LoginPassword)%>
                            <%= Html.ValidationMessageFor(model => model.LoginPassword)%>
                        </div>
                    </li><li>
                        <div class="checkbox">                
                            <%= Html.CheckBoxFor(model => model.Remember)%>
                            <%= Html.LabelFor(model => model.Remember)%>
                        </div>
                    </li><li>
                        <input type="submit" class="button" value="Login" id="btnLoginSubmit" />
                        <div id="divlogin_ajaxloading" style="display:none; vertical-align:top; text-align:center;"><img alt="" src="/images/ajax-loader.gif" /></div>
                    </li>
                </ul> 
            }

您的部分视图操作:

public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.LoginEmail, model.LoginPassword))
                {
                    return Redirect("/MyPage");
                }
                else
                {
                    ModelState.Clear();
                    return PartialView("LoginPopup", new LoginModel());
                }
            }
        }

for creating buttn you can also do this use HTML.ActionLink() or Ajax.ActionLink() defined as a button like:

<% Response.Write(Html.ActionLink("Add Product", "Create", new { id = tId, tNum = tNum }, new { @class = "oldVal" })); %>

it'll create a button on your page, and here you can see different attributes (so just check them out you'll find it useful) ... and from both of this which to use is more depends upon your desired action ...

and in your controller:

public ActionResult Create() 
{
    // do same as mentioned by [gnome][1]
}

here is the example: modal popup, declared as a partial view of page
[partial view using Ajax.ActionLink()]

using (Ajax.BeginForm("Login", "Users", null, new AjaxOptions() { UpdateTargetId = "divLoginPopupContent" }))
            {
                Response.Write(Html.ValidationSummary(true));
    %>
                <ul class="chooseQuestion">
                    <li>
                        <div class="short">
                            <%= Html.LabelFor(model => model.LoginEmail)%>
                        </div>
                        <div class="editor-field">
                            <%= Html.TextBoxFor(model => model.LoginEmail)%>
                            <%= Html.ValidationMessageFor(model => model.LoginEmail)%>
                        </div>
                    </li><li>
                        <div class="short">
                            <%= Html.LabelFor(model => model.LoginPassword)%>
                        </div>
                        <div class="editor-field">
                            <%= Html.PasswordFor(model => model.LoginPassword)%>
                            <%= Html.ValidationMessageFor(model => model.LoginPassword)%>
                        </div>
                    </li><li>
                        <div class="checkbox">                
                            <%= Html.CheckBoxFor(model => model.Remember)%>
                            <%= Html.LabelFor(model => model.Remember)%>
                        </div>
                    </li><li>
                        <input type="submit" class="button" value="Login" id="btnLoginSubmit" />
                        <div id="divlogin_ajaxloading" style="display:none; vertical-align:top; text-align:center;"><img alt="" src="/images/ajax-loader.gif" /></div>
                    </li>
                </ul> 
            }

your partial view action:

public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.LoginEmail, model.LoginPassword))
                {
                    return Redirect("/MyPage");
                }
                else
                {
                    ModelState.Clear();
                    return PartialView("LoginPopup", new LoginModel());
                }
            }
        }
柳若烟 2024-11-14 17:08:14

刚刚输入此内容,尚未对其进行测试,但基本思想是在客户视图上列出您的产品,并使用一个按钮为客户添加新产品。

列出客户的产品:

<table>
    <% foreach(var p in Customer.Products) { %>
    <tr>
        <td><%: p.Quantity %></td>
        <td><%: p.Name %></td>
    </tr>
    <% } %>
</table>
<p><%: Html.ActionLink("Add Product", "Create", new { controller = 'Products' }, new { id = 'addProduct'}) %>

<div id="dialog"></div>

要为客户添加新产品,您可以使用 jQuery UI 显示一个对话框;只需传递客户 ID

包含 jquery-ui

<script type="text/javascript">
$(document).ready(function() {
    $('#dialog').dialog({ 
        autoOpen : false,
        button : { 
            'Save' : function() {
                $.ajax({
                    url : $('#addProduct').attr('href'),
                    type : 'get'
                    success : function() { 
                        alert('Product added!');
                    }
                });
            }, 
            'Cancel' : function() {
                $('#dialog').dialog('close');
            }
        }
    });

    $('#addProduct').click(function() {
        var customerId = $('#CustomerId').val();
        $('#dialog').dialog('open');
        $('#Customer_Product_Id').val(customerId); // assuming there's a hidden field on the form
    });
});
</script>

// 假设您已在产品控制器中

public ActionResult Create() 
{
    List<Product> products = new List<Product>() { 
        new Product() { Id = 1, Name = "Rice" },
        new Product() { Id = 2, Name = 'Corn' }};
    ViewData.Add("Products", new SelectList(products, "Id", "Name", ""));
    Product product = new Product();
    if (Request.IsAjaxRequest()) 
    {
        return PartailView("_CreateOrEdit", product)
    }

    return View("Create", product);
}

返回部分_Create 视图

<%: Html.TextBoxFor(model => model.Quantity) %>
<%: Html.DropDownList("Products") %>
<%: Html.HiddenFieldFor(model => model.Product.Customer.Id) %>

Just typed this up, haven't tested it but the basic idea is to list your products on the customer view with a button to add a new product for a customer.

To list Products for Customer:

<table>
    <% foreach(var p in Customer.Products) { %>
    <tr>
        <td><%: p.Quantity %></td>
        <td><%: p.Name %></td>
    </tr>
    <% } %>
</table>
<p><%: Html.ActionLink("Add Product", "Create", new { controller = 'Products' }, new { id = 'addProduct'}) %>

<div id="dialog"></div>

To add a new Product for a customer you could use jQuery UI to show a dialog; just pass the Customer Id

// assuming you've included jquery-ui

<script type="text/javascript">
$(document).ready(function() {
    $('#dialog').dialog({ 
        autoOpen : false,
        button : { 
            'Save' : function() {
                $.ajax({
                    url : $('#addProduct').attr('href'),
                    type : 'get'
                    success : function() { 
                        alert('Product added!');
                    }
                });
            }, 
            'Cancel' : function() {
                $('#dialog').dialog('close');
            }
        }
    });

    $('#addProduct').click(function() {
        var customerId = $('#CustomerId').val();
        $('#dialog').dialog('open');
        $('#Customer_Product_Id').val(customerId); // assuming there's a hidden field on the form
    });
});
</script>

In your product controller return a partial

public ActionResult Create() 
{
    List<Product> products = new List<Product>() { 
        new Product() { Id = 1, Name = "Rice" },
        new Product() { Id = 2, Name = 'Corn' }};
    ViewData.Add("Products", new SelectList(products, "Id", "Name", ""));
    Product product = new Product();
    if (Request.IsAjaxRequest()) 
    {
        return PartailView("_CreateOrEdit", product)
    }

    return View("Create", product);
}

_Create view

<%: Html.TextBoxFor(model => model.Quantity) %>
<%: Html.DropDownList("Products") %>
<%: Html.HiddenFieldFor(model => model.Product.Customer.Id) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文