在按钮提交上使用(Html.BeginForm())发送类型为CategoriesLanguages的对象

时间:2014-01-31 09:32:53

标签: asp.net-mvc ajax.beginform

您好,我是Mvc的新手,我很高兴感谢任何帮助!

所以这是myModels

分类

-CategoryID

-DateCreated


CategoriesLanguages

  • ID(自动增量)

  • 类别ID

  • LanguageID

  • 标题

  • 描述

基本上我希望能够在单击AddNew Button时 - 我在Category表中创建一个新记录 - 并且我拥有所创建类别的id。 在Create()视图中返回Create()操作,我给了用户填写categorylanguages描述和标题的机会。

当用户单击提交按钮时,应将其重定向到Create(CategoryLanguages)acton,如您所见,它接受CategoryLanguage的对象,此操作将简单地将此对象存储在数据库中。我的问题是如何返回这个对象!

public class CategoryController : Controller
    {


        public ActionResult Create()
        {
            CategoryViewModel vm = new CategoryViewModel();
            vm.AddNewCategory();
            return View(vm);
        }
          pubcli AcrionResult Create(CategoryLanguage ob)
          {
              CategoryViewModel vm = new CategoryViewModel();
              vm.SaveInDatabaseCategorylanguage(ob);
              return RedirectToAction("Index");

          }
}

这是我的View CreateView.csHtml

 @model MvcApplication1.ViewModel.CategoryViewModel

  /
  @using (Html.BeginForm()) 
  {

    <fieldset class="form-horizontal">
        <legend>Category</legend>

        // Here i should have a dropdpwn and teh selected value I should get it for LanguageID

        <div class="control-group">
        @Html.LabelFor(model => model.modelcatlang.Title, new { @class = "control-label" })
            <div class="controls">
                @Html.EditorFor(model => model.modelcatlang.Title) //From here i should get title

            </div>
        </div>


        <div class="control-group">
            @Html.LabelFor(model => model.modelcatlang.Description, new { @class = "control-label" })
            <div class="controls">
                @Html.EditorFor(model => model.modelcatlang.Description) //from here I should get the description
            </div>
        </div>

        <div class="form-actions no-color">
            <input type="submit" value="Create" class="btn" />

/*somehow ewhen i click this button I should make 

CategoryLanguage  catlang= new CategoryLanguahe;

catLang.CatID = insertedID (I have it nio problem);

catlang.lanID = dropdown.value;

catlang.Title = from title from editorform
...*/

        </div>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

修改

我发布了My CategoryViewModel的代码

public class CategoryViewModel
    {
        public CategoryLanguages modelcatlang { get; set; }
        public int newCategoryID { get; set; }
        public List<Language> lstLanguages { get; set; }
        public List<CategoryLanguages> lstCategoryLanguages { get; set; }
        public CategoryLanguages categoryToEdit { get; set; }
        private readonly ICategoryRepository catRep;
        private readonly ILanguageRepository lanRep;
        private readonly ICategoryLanguageRepository catlanRep;
        public CategoryViewModel()
            : this(new CategoryRepository(), new LanguageRepository(), new CategoryLanguageRepository())
        {

        }

        public CategoryViewModel(ICategoryRepository catRep, ILanguageRepository lanRep, ICategoryLanguageRepository catlanRep)
        {
            this.catRep = catRep;
            this.lanRep = lanRep;
            this.catlanRep = catlanRep;
        }


        public void AddNewCategory()
        {
            lstLanguages = lanRep.GetAllAvailableLanguages();
            newCategoryID = catRep.AddCategory();
            modelcatlang = new CategoryLanguages();
        }

所以在AddNewCategory()方法的ViewModel中,我有类别表中刚刚插入的类别的id(在newCategoryID中) 我也创建了一个CategoryLanguage()的实例。也许我的新问题是 - 如何填充modelcatlang对象的所有属性(我的viewmodel的一部分)并将其返回给Create(CategoriesLanguages)动作

0 个答案:

没有答案