Uable使用在同一Mvc4 code这MVC3工作 [英] Uable to use same code in Mvc4 which worked in mvc3

查看:163
本文介绍了Uable使用在同一Mvc4 code这MVC3工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery是

 <script type="text/javascript">
    $(document).ready(function () {
        $('p').click(function () {
            var textbox = $('<input id="Text1" type="text"  name="Name" />')
            var oldText = $(this).text();
            $(this).replaceWith(textbox);
            textbox.blur(function () {
                var newValue = $(this).val();
                $(this).replaceWith($('<p>' + newValue + '</p>').after($('<input id="Text1" type="text" name="Name" />',{ type: 'hidden', name: 'Name', value: newValue })));
            });
            textbox.val(oldText);
        });

    }); 

</script>

,然后有2个控制器动作(GET和POST):

and then have the 2 controller actions (GET and POST):

public ActionResult Viewdetails()
{
    User ur = new User();
    ur.Name = "Danny";
    return View(ur);
}

[HttpPost]
public ActionResult Display(User model)
{
    return View(model);
}

和里面的 Viewdetails.cshtml

@model User
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
    <div>Name: <a>@Model.Name</a><br /></div>
    <input type="submit" value="Submit" /> 
    <br />
}

和里面的 Display.cshtml

@model User
<div>You have selected: @Model.Name</div>

我无法运行这MVC3正常工作期间编辑名称

I am unable to edit Name during runtime which worked fine in mvc3

推荐答案

尝试这样的:

textbox.blur(function () {
    var newValue = $(this).val();
    $(this).replaceWith(
        $('<a />', { text: newValue })
        .after(
            $('<input />', { type: 'hidden', name: 'Name', value: newValue })
        )
    );
});


更新:

完整的示例:

型号:

public class User
{
    public string Name { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        User ur = new User();
        ur.Name = "Danny";
        return View(ur);
    }

    [HttpPost]
    public ActionResult Index(User model)
    {
        return Content(model.Name);
    }
}

查看(〜/查看/主页/ Index.cshtml

@model AppName.Models.User
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $('a').click(function () {
            var textbox = $('<input id="Text1" type="text" name="Name" />')
            var oldText = $(this).text();
            $(this).replaceWith(textbox);
            textbox.blur(function () {
                var newValue = $(this).val();
                $(this).replaceWith(
                    $('<a/>', { text: newValue })
                    .after(
                        $('<input />', { type: 'hidden', name: 'Name', value: newValue })
                    )
                );
            });
            textbox.val(oldText);
        });
    }); 
    </script>
</head>
<body>
    @using (Html.BeginForm())
    {
        <div>Name:<a>@Html.DisplayFor(x => x.Name)</a></div>
        <input type="submit" value="Submit" /> 
    }
</body>
</html>

这篇关于Uable使用在同一Mvc4 code这MVC3工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆