Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何从视图中调用方法?MVC_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 如何从视图中调用方法?MVC

Asp.net mvc 如何从视图中调用方法?MVC,asp.net-mvc,Asp.net Mvc,当用户单击“创建”时,我有一个带有的视图,应该激活操作方法,并将结果写入数据库 当用户在视图中单击“创建”按钮时,不会发生任何事情。你能告诉我我做错了什么吗?谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TestGuestBook.Models; using TestGuestBook.Models.Repos

当用户单击“创建”时,我有一个带有
的视图,应该激活操作方法,并将结果写入数据库

当用户在视图中单击“创建”按钮时,不会发生任何事情。你能告诉我我做错了什么吗?谢谢

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestGuestBook.Models;
using TestGuestBook.Models.Repositories;
using TestGuestBook.ViewModels;

namespace TestGuestBook.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {

        ICommentRepository _repository;

        public HomeController()
        {
            _repository = new CommentRepository();
        }

        // Dependency Injection enabled constructors
        public HomeController(ICommentRepository repository)
        {
            _repository = repository;
        }

        public ActionResult Index()
        {
            // Get all Comments
            List<Comment> commentItems = _repository.FindAll().ToList();
            // Create the ViewModel and associate the list of comments
            CommentListCreateViewModel viewModel = new CommentListCreateViewModel();
            viewModel.CommentItems = commentItems;

            return View(viewModel);
        }

        public ActionResult Create()
        {
            CommentListCreateViewModel createViewModel = new CommentListCreateViewModel();
            return View(createViewModel);
        }

        [HttpPost]
        public ActionResult Create(CommentListCreateViewModel createViewModel)
        {
            if (ModelState.IsValid)
            {
                Comment comment = new Comment
                {
                    Nominative = createViewModel.Nominative,
                    Email = createViewModel.Email,
                    Content = createViewModel.Content
                };
                _repository.Add(comment);
                _repository.Save();
            }
            return View();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用TestGuestBook.Models;
使用TestGuestBook.Models.Repositories;
使用TestGuestBook.ViewModels;
命名空间TestGuestBook.Controllers
{
[手机错误]
公共类HomeController:控制器
{
ICommentRepository\u存储库;
公共家庭控制器()
{
_repository=新的CommentRepository();
}
//支持依赖注入的构造函数
公共HomeController(ICommentRepository存储库)
{
_存储库=存储库;
}
公共行动结果索引()
{
//获取所有评论
List commentItems=_repository.FindAll().ToList();
//创建ViewModel并关联注释列表
CommentListCreateViewModel viewModel=新CommentListCreateViewModel();
viewModel.CommentItems=CommentItems;
返回视图(viewModel);
}
公共操作结果创建()
{
CommentListCreateViewModel createViewModel=新CommentListCreateViewModel();
返回视图(createViewModel);
}
[HttpPost]
公共操作结果创建(CommentListCreateViewModel createViewModel)
{
if(ModelState.IsValid)
{
注释=新注释
{
Nagnative=createViewModel.Nagnative,
Email=createViewModel.Email,
Content=createViewModel.Content
};
_添加(注释);
_Save();
}
返回视图();
}
}
}

看法

@model TestGuestBook.ViewModels.CommentListCreateViewModel
@{
ViewBag.Title=“Index”;
}
指数
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
ListAddCommentsViewModel
@LabelFor(model=>model.naminative)
@EditorFor(model=>model.naminative)
@Html.ValidationMessageFor(model=>model.namignive)
@LabelFor(model=>model.Email)
@EditorFor(model=>model.Email)
@Html.ValidationMessageFor(model=>model.Email)
@LabelFor(model=>model.Content)
@EditorFor(model=>model.Content)
@Html.ValidationMessageFor(model=>model.Content)

} 主格的 电子邮件 内容 @foreach(Model.CommentItems中的var项) { @DisplayFor(modelItem=>item.Naminative) @DisplayFor(modelItem=>item.Email) @DisplayFor(modelItem=>item.Content) }
您需要将表单指向您的
创建
控制器方法:

@using (Html.BeginForm("Create", "Home"))

您可以将其保留为Html.BeginForm(),保存后,调用return RedirectToAction(“Index”);添加的项目现在应显示在列表中。它可能一直在保存,只是后来没有被重新定向到索引视图。

使用(Html.BeginForm())替换我发布的内容。如果不在表单中指定操作,它将发回同一URL,并将简单地调用索引操作。我这样做了,现在我收到一个错误,视图“创建”或其主视图未找到,或者没有视图引擎支持搜索的位置。已搜索以下位置:。。。我不确定我做错了什么。我需要在同一个URL上发布我的想法是在同一页面上显示项目列表和创建新项目的web表单它调用
Create
方法并尝试显示Create视图,该视图必须不存在。在
Create
HttpPost方法中,可以更改方法以显示索引视图:
returnview(“Index”)表单正在发布到索引,因此单独保留
BeginForm
将不起作用。啊,我没有注意到这是索引视图,而不是创建视图。:-)
@using (Html.BeginForm("Create", "Home"))