你好,因为我对 Mvc 很陌生,我很感激任何帮助!
所以这是我的模型
类别
-类别ID
-创建日期
类别语言
ID(自动增量)
类别ID
语言ID
标题
描述
基本上,当我单击 AddNew 按钮时,我希望能够 - 我在 Category 表中创建了一条新记录 - 并且我拥有所创建类别的 id。在 Create() 视图中 Create() 操作正在返回,我让用户有机会填写类别语言的描述和标题。
并且当用户单击提交按钮时,他应该被重定向到 Create(CategoryLanguages) 动作,如您所见,该动作接受 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");
}
}
这是我的视图 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 对象(我的视图模型的一部分)并将其返回到 Create(CategoriesLanguages) 操作