将值从视图传递到Controller N然后传递到其他View

时间:2010-11-19 01:55:44

标签: asp.net-mvc visual-studio-2010

顾客:                        
<p></p>
<p></p>
<div> <% foreach (var item in Model)
         { %>
         You are viewing Users of Customer: <%:item.Customer %>
         <%break; %>
         <%} %></div>
    <p></p>
    <% Html.RenderPartial("EditUsers", Model); %>

<p>
    <%: Html.ActionLink("Create New", "Create", "Profile", null, null)%>
</p>

现在我要传递&lt;%:item.Customer%&gt;通过%:Html.ActionLink(“创建新”,“创建”,“配置文件”,null,null)%&gt;这应该显示在“创建”视图中 这是控制器,我也将给出创建视图

控制器: public ActionResult Create()         {             return View();         }

    [HttpPost]
    public ActionResult Create(string UserName, string Password, string FirstName, string LastName,
        string MiddleInitial,  string Email,string Telephone,  bool IsAdmin, bool IsSubAdmin)
    {
        UserDAL userDALObject = new UserDAL();
        tblUser newUser = new tblUser();

        newUser.Customer = customerNumber; 
        newUser.UserName = UserName;
        newUser.Password = Password;
        newUser.FirstName = FirstName;
        newUser.LastName = LastName;
        newUser.MiddleInitial = MiddleInitial;
        newUser.Email = Email;
        newUser.Telephone = Telephone; 

        newUser.IsAdmin = IsAdmin;
        newUser.IsSubAdmin = IsSubAdmin;

        userDALObject.AddUserDetails(newUser);
        TempData["UserCreationMsg"] = string.Format("User named :{0}, is created",UserName);
        return View();
    }
    public ActionResult EditUser(string id)
    {
        UserDAL userDALObject = new UserDAL();
        tblUser userDetails = userDALObject.GetUser(Int32.Parse(id));
        TempData["EditUserId"] = id;
        return View(userDetails);
    }

创建视图 &lt;%if(TempData [“UserCreationMsg”]!= null)%&gt;     &lt;%{%&gt;         &lt;%:TempData [“UserCreationMsg”]。ToString()%&gt;     &lt;%}%&gt;

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>          


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

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

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

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

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

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

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


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

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

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

1 个答案:

答案 0 :(得分:1)

你的问题究竟是什么?

在侧面,您应该使用模型绑定将person对象传递给CreateView操作方法,而不是单独传递每个属性。它会减少很多代码。搜索模型绑定以获取更多详细信息。