如何将所选下拉列表的值从视图传递到MVC中的控制器

时间:2019-06-13 16:12:50

标签: c# asp.net-mvc

我需要一些有关MVC的帮助,这是该领域的新功能。这是我的下拉列表或选择选项的代码,我只想由用户在控制器中获取选定的值,以便可以将该值传递给db中的表。这是来自Model和controller和.cshtml

的代码

模型课堂

public class Book
  {
    public int BookID { get; set; }
    public string BookName { get; set; }
    public string author { get; set; }
    public BookType BookType { get; set; }
 }

 public class BookType
 {
    public int TypeId { get; set; }
    public string Name { get; set; }
    public List<Book> Books { get; set; }
   }

和控制器

  public ActionResult Create()
    {
        //ViewData["BookType"] = context.BookType.ToList();
        ViewBag.BookType = new SelectList(context.BookType,"TypeId", "Name", context.BookType);

        return Json(new
        {
            d = true,
            pv = RazorToString(this, "~/views/Book/Create.cshtml",null),


        }, JsonRequestBehavior.AllowGet);

    }


    [HttpPost]
    public ActionResult Create(Book book)
    {
        try
        {
            if (ModelState.IsValid)
            {
                context.Book.Add(book);
                context.SaveChanges();
                var books = context.Book.ToList();

                return Json(new
                {
                    d = true,
                    pv = RazorToString(this, "~/views/Book/list.cshtml", books),

                }, JsonRequestBehavior.AllowGet);
            }
        }
        catch (DataException /* dex */)
        {
            ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
        }

        return View(book);
    }

create.cshtml页面

   @{ 
    var types =   ViewBag.BookType  as SelectList;

 }
 @using (Ajax.BeginForm("Create", "book", new AjaxOptions { OnSuccess = 
"onCreateSuccess" }))
{

<div class="row">
    <div class="col-sm-3">
        <div class="form-group">
            <label for="txtBook_Name">Book Name:</label>

            <input type="text" required name="BookName" id="txtBook_Name" placeholder="Book Name" class="big-input">

        </div>


        <div class="form-group">
            <label for="txtauthor">author:</label>
            <input type="text" required name="author" id="txtauthor" placeholder="author" class="big-input">

        </div>
        <div class="form-group">
            @if (types != null)
            {

                <select name="BookType">
                    <option value="value">select type</option>

                    @foreach (var item in types)
                    {
                        <option value="@item.Value">@item.Text</option>
                    }

                </select>
            }



        </div>
        <div>
            <div class="pull-right">
                <input type="submit" value="Add" class="btn btn-primary" />
                <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>

 }

0 个答案:

没有答案
相关问题