无法在同一视图中为另一个按钮运行另一个功能

时间:2013-07-25 08:40:52

标签: c# asp.net-mvc asp.net-mvc-4

我在同一个视图中有两个按钮,一个正在使用数据显示在另一个视图的标签中,我已经为button2编写了函数(添加了另一个值),当我点击button2时它没有在视图中显示数据.....而不是像这样给出错误... http:404资源未找到错误

这是视图

@model MvcSampleApplication.Models.products    
@{
    ViewBag.Title = "Valuesadd";
}    
<h2>Valuesadd</h2>    
@using (Html.BeginForm("SubmitValue","EnterValue",FormMethod.Post))
{
<div>
   <fieldset>
        <legend>Enter Textbox Value</legend>
        <div class ="editor-label">
            @Html.LabelFor(m => m.EnteredValue)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m=>m.EnteredValue)
        </div>
        <p>           
            <input type="submit" value="Submit1" />                     
        </p>
    </fieldset>
  </div>
}
@using (Html.BeginForm("SubmitValue2","EnterValue",FormMethod.Post))
{
    <p>
   <input type="submit" value="Submit2" />
    </p>
}

这是

的控制器
namespace MvcSampleApplication.Controllers
{
    public class EnterValueController : Controller
    {           
        [HttpPost]
        public ActionResult SubmitValue(MvcSampleApplication.Models.products model)
        {
            TempData["logindata"] = model.EnteredValue;
            return RedirectToAction("submittedvalues" , "SubmitValue2"); 
          // how can we redirect to another view when the button is clicked in one view          
        }             
        public ActionResult submittedvalues()
        {
            var model = new MvcSampleApplication.Models.category();
            string data = TempData["logindata"] != null ? TempData["logindata"].ToString() : "";
            model.lablvalue = data;
            return View(model);        
        }
        // action for second button click 
        public ActionResult submittedvalues2()
        {
            var model = new MvcSampleApplication.Models.category();
            string data = TempData["logindata"] != null ? TempData["logindata"].ToString() : "";
            model.lablvalue = "HIIII"+data;
            return View(model);
        }
    }
}
你会建议任何想法吗? 非常感谢......

1 个答案:

答案 0 :(得分:3)

控制器中的表单操作和操作名称不相同。你也没有HttpPost

@using (Html.BeginForm("SubmitValue2","EnterValue",FormMethod.Post))
{
}

//add this
[HttpPost]
public ActionResult submittedvalues2()
{
    var model = SOMETHING;
    return View("submittedvalues", model);
}

    [HttpPost]
    public ActionResult submittedvalues2()
    {
        //Do your post actions and redirect to action
        return RedirectToAction("submittedvalues");
    }
表单中的

SubmitValue2 应为 submittedvalues2 ,并在其上添加HttpPost属性