在局部视图中预填充字段创建 [英] Prepopulate fields in partial view create

查看:50
本文介绍了在局部视图中预填充字段创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



MVC开发新手。努力为父视图中的预填充字段值加载创建的部分视图。



基本上我有一个主视图,在主视图中我有一个局部视图,它显示一个带有编辑按钮和添加按钮的网格。



这就是我在父视图中加载我的部分视图的方式。

Hi all,

New to MVC development. Struggling to load a partial view for 'Create' with prepopulated field values from Parent View.

Basically I have a main View, and within the Main view I have a partial view which displays a grid with Edit button and Add button.

This is how I am loading my Partial view in my Parent view.

var url = '@Url.Action("Index", "MyModel")';
$.ajax({
    url: url ,
    type: "get",
    success: function (data) {
        $('#partialPlaceHolder').html(data);
        $('#partialPlaceHolder').fadeIn('fast');
    },
    error: function (xhr, status, error) {
        alert(xhr.responseText);
    }
});





编辑按钮加载'编辑'视图,添加按钮加载'创建'视图,非常标准的MVC 。



但现在我的主要问题是,我想在使用父视图中的值单击添加按钮时预填充创建视图。因此用户不必再次将值输入其中。



我的模特由3件物品组成





The Edit button loads the 'Edit' View and the Add button loads the 'Create' view, pretty standard MVC.

But now my main issue is, I want to prepopulate the 'Create' view when 'Add' button is clicked with values from the Parent View. So the users dont have to input the values into them again.

My model consists of 3 items

 public class MyModel
    {

        public int ID { get; set; }

        [DisplayName("My Note")]      
        [Required(ErrorMessage = "Note cannot be left blank.")]
        public string MyNote { get; set; }
               
        [DisplayName("My Value")]
        [Required(ErrorMessage = "Value is required")]
        [StringLength(12, MinimumLength = 12)]
        public string MyValue { get; set; }

        [DisplayName("My List")]
        [Required(ErrorMessage = "Please select an item.")]
        public int MyList{ get; set; }
    
        public virtual MyListModel MyLists { get; set; }
}





单击添加按钮后,我希望创建视图预先填充我的价值 '和'我的列表'项目来自父视图,并在加载时填充'创建'视图。因此,用户只需填写我的笔记文本框,然后单击创建按钮即可保存更改。这样的事情可能吗?任何帮助将不胜感激。



以下是我的 - 创建控制器中的动作结果代码。



我尝试过:





When the Add button is clicked, I want the 'Create' view to pre populate the 'My Value' and 'My List' item from the Parent View and populate the 'create' view when its loaded. So the user would only have to fill in the 'My notes' text box and click on 'Create' button to save the changes. Is something like this possible? Any help would be appreciated.

Below is the Action result code in my - Create Controller.

What I have tried:

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Create(MyModel mymodel)
{
    try
    {
        if (ModelState.IsValid)
        {

            try
            {

                db.MyModel.Add(mymodel);
                db.SaveChanges();
                //Populating droplist
                ViewBag.MyList = new SelectList(db.MyList, "ID", "MyText", mymodel.MyList);
                return Json(new { success = true });
            }
            catch (Exception e)
            {
                throw e;
            }

        }


        return Json(new
        {
            success = false,
            responseText = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                          .Select(m => m.ErrorMessage).ToList()
        });
    }
    catch (Exception ex)
    {

        return Json(new { success = false, responseText = "Error creating new note. " + ex.Message });
    }

}

推荐答案

.ajax({
url:url,
type:get,
success:function(data){
.ajax({ url: url , type: "get", success: function (data) {


('#partialPlaceHolder')。html(data);
('#partialPlaceHolder').html(data);


('#partialPlaceHolder')。fadeIn('fast');
},
错误:function(xhr,status,error){
alert( xhr.responseText);
}
});
('#partialPlaceHolder').fadeIn('fast'); }, error: function (xhr, status, error) { alert(xhr.responseText); } });





编辑按钮加载'编辑'视图,添加按钮加载'创建'视图,非常标准的MVC 。



但现在我的主要问题是,我想在使用父视图中的值单击添加按钮时预填充创建视图。因此用户不必再次将值输入其中。



我的模特由3件物品组成





The Edit button loads the 'Edit' View and the Add button loads the 'Create' view, pretty standard MVC.

But now my main issue is, I want to prepopulate the 'Create' view when 'Add' button is clicked with values from the Parent View. So the users dont have to input the values into them again.

My model consists of 3 items

 public class MyModel
    {

        public int ID { get; set; }

        [DisplayName("My Note")]      
        [Required(ErrorMessage = "Note cannot be left blank.")]
        public string MyNote { get; set; }
               
        [DisplayName("My Value")]
        [Required(ErrorMessage = "Value is required")]
        [StringLength(12, MinimumLength = 12)]
        public string MyValue { get; set; }

        [DisplayName("My List")]
        [Required(ErrorMessage = "Please select an item.")]
        public int MyList{ get; set; }
    
        public virtual MyListModel MyLists { get; set; }
}





单击添加按钮后,我希望创建视图预先填充我的价值 '和'我的列表'项目来自父视图,并在加载时填充'创建'视图。因此,用户只需填写我的笔记文本框,然后单击创建按钮即可保存更改。这样的事情可能吗?任何帮助将不胜感激。



以下是我的 - 创建控制器中的动作结果代码。



我尝试过:





When the Add button is clicked, I want the 'Create' view to pre populate the 'My Value' and 'My List' item from the Parent View and populate the 'create' view when its loaded. So the user would only have to fill in the 'My notes' text box and click on 'Create' button to save the changes. Is something like this possible? Any help would be appreciated.

Below is the Action result code in my - Create Controller.

What I have tried:

[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Create(MyModel mymodel)
{
    try
    {
        if (ModelState.IsValid)
        {

            try
            {

                db.MyModel.Add(mymodel);
                db.SaveChanges();
                //Populating droplist
                ViewBag.MyList = new SelectList(db.MyList, "ID", "MyText", mymodel.MyList);
                return Json(new { success = true });
            }
            catch (Exception e)
            {
                throw e;
            }

        }


        return Json(new
        {
            success = false,
            responseText = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                          .Select(m => m.ErrorMessage).ToList()
        });
    }
    catch (Exception ex)
    {

        return Json(new { success = false, responseText = "Error creating new note. " + ex.Message });
    }

}


这篇关于在局部视图中预填充字段创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆