0

我在同一个视图中有两个按钮,一个正在处理要在另一个视图的标签中显示的数据,我已经为 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);
        }
    }
}

你能提出任何想法吗..非常感谢...

4

1 回答 1

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应该是submitvalues2,并在其上添加一个HttpPost属性

于 2013-07-25T08:49:36.133 回答