小编kis*_*pit的帖子

为什么操作方法DeleteConfirmed使用模型id而不是模型对象作为其参数?

我是ASP.NET MVC的新手,现在通过阅读asp.net中给出的教程从零开始学习.我的问题可能太简单了,但我找不到答案.为了快速回复,我在这里问.

编辑操作方法:

        // GET: /Movie/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        //
        // POST: /Movie/Edit/5

        [HttpPost]
        public ActionResult Edit(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movie).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(movie);
        }
Run Code Online (Sandbox Code Playgroud)

删除操作方法:

        //
        // GET: /Movie/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        //
        // …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-mvc-4

1
推荐指数
1
解决办法
989
查看次数

为什么在使用 POST 动词提交表单时会再次调用 HttpGet 方法?

控制器:

// other usings are removed for the sake of brevity
using MvcMovie.Models;

namespace MvcMovie.Controllers
{
    public class MoviesController : Controller
    {
        private MoviewDBContext db = new MoviewDBContext();

        public ActionResult SearchIndex(string searchString)
        {
            var movies = db.Movies.Select(x => x);
            if (!string.IsNullOrEmpty(searchString))
                movies = movies.Where(x => x.Title.Contains(searchString));

            return View(movies);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

看法:

// SearhcIndex.cshtml

@model IEnumerable<MvcMovie.Models.Movie>

@using (Html.BeginForm())
{
    <p>
        Title: @Html.TextBox("searchString")
        <br />
        <input type="submit" value="Filter" />
    </p>
}

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
    </tr>

    @foreach (var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc

1
推荐指数
1
解决办法
1081
查看次数

无论.NET版本如何,对具有相同种子的Random.Next()的第n次调用是否总是产生相同的数字?

我是一个新手C#学习者,并构建一个系统,从一组有序的问题生成问题表.每个问题表都有30个问题从问题集中随机选择.每个问题表由一个整数唯一标识,该整数用作种子值.

由于我不知道随机生成器如何工作的内部细节,我想知道无论.NET版本如何,使用相同种子的n-th调用是否Random.Next()总是产生相同的数字.

最小的工作示例如下.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int seed = 12345;
            Random rnd = new Random(seed);
            for (int x = 0; x < 10; x++)
                Console.WriteLine(rnd.Next());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# versioning random

1
推荐指数
1
解决办法
138
查看次数

如何在多行TextBox上打印ping.exe的进度?

我有一个文本框和一个按钮.该按钮调用Ping(),我希望显示ping的任何进度textBox1.

        void Ping()
        {
            p = new Process();
            p.StartInfo.FileName = "ping";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.Arguments = "www.microsoft.com";
            p.Start();
            textBox1.Text = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
        }
Run Code Online (Sandbox Code Playgroud)

怎么实现呢?我上面写的当前实现并未显示进度,而是显示最终输出.

c#

1
推荐指数
1
解决办法
1210
查看次数

为什么不能为数组的参数和元素省略new int []和new []?

static void Main(string[] args)
{
    int[][] data =// new int[][] has been omitted here!
    {
        new int[] {1,2,3},// new int[] cannot be omitted
        new int[] {1,2,3,4}// new int[] cannot be omitted
    };

    Foo(new[] { 1, 2, 3 });// new[] cannot be omitted
}

static void Foo(int[] data) {/* implementation omitted */}
Run Code Online (Sandbox Code Playgroud)

为什么不能new int[]和new[]对参数和数组元素可以省略?

c#

1
推荐指数
1
解决办法
97
查看次数

如何全局获取一次HTML元素并多次使用它?

我不想调用

        var element = document.getElementById('ph');
Run Code Online (Sandbox Code Playgroud)

每次UpdateClock为了效率而被召唤.所以我试图把它放在全球而不是放在里面UpdateClock.但是,它不起作用.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>This is a title</title>
    <script>
        var element = document.getElementById('ph');

        function UpdateClock() {
            var now = new Date();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();

            if (hours < 10)
                hours = "0" + hours;
            if (minutes < 10)
                minutes = "0" + minutes;
            if (seconds < 10)
                seconds = "0" + seconds;
            element.innerHTML = hours + ":" + minutes + ":" …
Run Code Online (Sandbox Code Playgroud)

javascript

1
推荐指数
1
解决办法
30
查看次数

扩展方法无法识别,有什么不对?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;

namespace MvcMusicStore.Controllers
{
    public class StoreController : Controller
    {
        //
        // GET: /Store/

        public string Index()
        {
            // ERROR call extension method here
            return GetMemberName();
        }
    }

    public static class Utilities
    {
        public static string GetMemberName(this Controller caller, [CallerMemberName] string memberName = "")
        {
            return caller.GetType().FullName + "." + memberName;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的扩展方法无法识别,有什么问题?

c#

0
推荐指数
1
解决办法
1071
查看次数

哪一个在性能上更有效,"x> = a"或"x> a || x == a"?

我不知道以下内部实现.简而言之,哪一个在性能上更有效,x>=a或者x>a || x==a?

编辑1:

我们将其分为两种情况:

  • 当两个a和x是在同一类型.

  • 何时a和x不属于同一类型.

c#

0
推荐指数
1
解决办法
87
查看次数

为什么我需要在以下字符串中加倍{但单个}?

为什么我需要在以下字符串中加倍{但只有一个}?

static void Main()
{

    Console.Write("a={0}, b={1}, c={{", 1, 2);
    foreach (var i in Enumerable.Range(1, 5)) Console.Write("{0},",i);
    Console.WriteLine("\b}");
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

c#

0
推荐指数
1
解决办法
57
查看次数

如何将字符串数组转换为句子?

如何使用Linq查询方法将字符串数组转换为句子?

private static void Main()
{
    string sentence = "C# is fun.";
    string[] words = sentence.Split();
    //string temp= words;       
}
Run Code Online (Sandbox Code Playgroud)

在temp希望有相同的值sentence.

c#

-1
推荐指数
1
解决办法
1568
查看次数

方法名称不能与其左括号分开

无法渲染

    <p>
        Your name: @Html.TextBoxFor
        (x => x.Name)
    </p>
Run Code Online (Sandbox Code Playgroud)

渲染

    <p>
        Your name: @Html.TextBoxFor(
        x => x.Name)
    </p>
Run Code Online (Sandbox Code Playgroud)

为什么我们不能将方法名称与左括号分开,(以便每个都有自己的行?

是否有一个技巧可以绕过这个功能(或可能是一个错误)?

c# asp.net-mvc razor

-1
推荐指数
1
解决办法
52
查看次数

标签 统计

c# ×10

asp.net-mvc ×3

asp.net-mvc-4 ×1

javascript ×1

random ×1

razor ×1

versioning ×1